Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
48.19% covered (danger)
48.19%
890 / 1847
15.15% covered (danger)
15.15%
5 / 33
CRAP
0.00% covered (danger)
0.00%
0 / 1
AppController
48.10% covered (danger)
48.10%
887 / 1844
15.15% covered (danger)
15.15%
5 / 33
51259.77
0.00% covered (danger)
0.00%
0 / 1
 processSGF
98.11% covered (success)
98.11%
52 / 53
0.00% covered (danger)
0.00%
0 / 1
15
 xFlip
75.00% covered (warning)
75.00%
3 / 4
0.00% covered (danger)
0.00%
0 / 1
2.06
 yFlip
75.00% covered (warning)
75.00%
3 / 4
0.00% covered (danger)
0.00%
0 / 1
2.06
 getInitialPositionEnd
100.00% covered (success)
100.00%
8 / 8
100.00% covered (success)
100.00%
1 / 1
3
 getInitialPosition
100.00% covered (success)
100.00%
20 / 20
100.00% covered (success)
100.00%
1 / 1
6
 getDeletedSets
0.00% covered (danger)
0.00%
0 / 7
0.00% covered (danger)
0.00%
0 / 1
12
 startPageUpdate
88.89% covered (warning)
88.89%
16 / 18
0.00% covered (danger)
0.00%
0 / 1
5.03
 saveSolvedNumber
78.12% covered (warning)
78.12%
25 / 32
0.00% covered (danger)
0.00%
0 / 1
13.51
 resetUserElos
0.00% covered (danger)
0.00%
0 / 14
0.00% covered (danger)
0.00%
0 / 1
12
 handleContribution
52.94% covered (warning)
52.94%
9 / 17
0.00% covered (danger)
0.00%
0 / 1
2.42
 getAllTags
96.55% covered (success)
96.55%
28 / 29
0.00% covered (danger)
0.00%
0 / 1
7
 encrypt
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
1 / 1
1
 checkPictureLarge
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
12
 checkPicture
66.67% covered (warning)
66.67%
2 / 3
0.00% covered (danger)
0.00%
0 / 1
3.33
 saveDanSolveCondition
100.00% covered (success)
100.00%
18 / 18
100.00% covered (success)
100.00%
1 / 1
7
 updateSprintCondition
94.44% covered (success)
94.44%
17 / 18
0.00% covered (danger)
0.00%
0 / 1
4.00
 updateGoldenCondition
94.12% covered (success)
94.12%
16 / 17
0.00% covered (danger)
0.00%
0 / 1
3.00
 setPotionCondition
0.00% covered (danger)
0.00%
0 / 14
0.00% covered (danger)
0.00%
0 / 1
6
 updateGems
3.85% covered (danger)
3.85%
4 / 104
0.00% covered (danger)
0.00%
0 / 1
2183.48
 checkProblemNumberAchievements
42.39% covered (danger)
42.39%
39 / 92
0.00% covered (danger)
0.00%
0 / 1
166.38
 checkDanSolveAchievements
46.82% covered (danger)
46.82%
81 / 173
0.00% covered (danger)
0.00%
0 / 1
545.62
 checkForLocked
0.00% covered (danger)
0.00%
0 / 5
0.00% covered (danger)
0.00%
0 / 1
20
 checkNoErrorAchievements
50.77% covered (warning)
50.77%
33 / 65
0.00% covered (danger)
0.00%
0 / 1
51.48
 checkTimeModeAchievements
35.40% covered (danger)
35.40%
40 / 113
0.00% covered (danger)
0.00%
0 / 1
2273.83
 checkRatingAchievements
54.44% covered (warning)
54.44%
49 / 90
0.00% covered (danger)
0.00%
0 / 1
95.92
 checkLevelAchievements
72.22% covered (warning)
72.22%
65 / 90
0.00% covered (danger)
0.00%
0 / 1
42.63
 checkSetCompletedAchievements
19.72% covered (danger)
19.72%
14 / 71
0.00% covered (danger)
0.00%
0 / 1
166.54
 setAchievementSpecial
0.00% covered (danger)
0.00%
0 / 184
0.00% covered (danger)
0.00%
0 / 1
4160
 checkSetAchievements
18.10% covered (danger)
18.10%
40 / 221
0.00% covered (danger)
0.00%
0 / 1
2533.09
 updateXP
60.00% covered (warning)
60.00%
6 / 10
0.00% covered (danger)
0.00%
0 / 1
3.58
 handleSearchSettings
100.00% covered (success)
100.00%
13 / 13
100.00% covered (success)
100.00%
1 / 1
2
 signIn
83.33% covered (warning)
83.33%
5 / 6
0.00% covered (danger)
0.00%
0 / 1
2.02
 beforeFilter
85.40% covered (warning)
85.40%
275 / 322
0.00% covered (danger)
0.00%
0 / 1
78.14
 afterFilter
n/a
0 / 0
n/a
0 / 0
1
1<?php
2
3App::uses('Auth', 'Utility');
4App::uses('TsumegoFilters', 'Utility');
5App::uses('TimeMode', 'Utility');
6
7class AppController extends Controller {
8    public $viewClass = 'App';
9
10    public $components = [
11        'Session',
12        //'DebugKit.Toolbar',
13        'Flash',
14        'PlayResultProcessor'
15    ];
16
17    public static function processSGF($sgf) {
18        $aw = strpos($sgf, 'AW');
19        $ab = strpos($sgf, 'AB');
20        $boardSizePos = strpos($sgf, 'SZ');
21        $boardSize = 19;
22        $sgfArr = str_split($sgf);
23        if ($boardSizePos !== false) {
24            $boardSize = $sgfArr[$boardSizePos + 3] . '' . $sgfArr[$boardSizePos + 4];
25        }
26        if (substr($boardSize, 1) == ']') {
27            $boardSize = substr($boardSize, 0, 1);
28        }
29
30        $black = AppController::getInitialPosition($ab, $sgfArr, 'x');
31        $white = AppController::getInitialPosition($aw, $sgfArr, 'o');
32        $stones = array_merge($black, $white);
33
34        $board = [];
35        for ($i = 0; $i < 19; $i++) {
36            $board[$i] = [];
37            for ($j = 0; $j < 19; $j++) {
38                $board[$i][$j] = '-';
39            }
40        }
41        $lowestX = 18;
42        $lowestY = 18;
43        $highestX = 0;
44        $highestY = 0;
45        $stonesCount = count($stones);
46        for ($i = 0; $i < $stonesCount; $i++) {
47            if ($stones[$i][0] < $lowestX) {
48                $lowestX = $stones[$i][0];
49            }
50            if ($stones[$i][0] > $highestX) {
51                $highestX = $stones[$i][0];
52            }
53            if ($stones[$i][1] < $lowestY) {
54                $lowestY = $stones[$i][1];
55            }
56            if ($stones[$i][1] > $highestY) {
57                $highestY = $stones[$i][1];
58            }
59        }
60        if (18 - $lowestX < $lowestX) {
61            $stones = AppController::xFlip($stones);
62        }
63        if (18 - $lowestY < $lowestY) {
64            $stones = AppController::yFlip($stones);
65        }
66        $highestX = 0;
67        $highestY = 0;
68        $stonesCount = count($stones);
69        for ($i = 0; $i < $stonesCount; $i++) {
70            if ($stones[$i][0] > $highestX) {
71                $highestX = $stones[$i][0];
72            }
73            if ($stones[$i][1] > $highestY) {
74                $highestY = $stones[$i][1];
75            }
76            $board[$stones[$i][0]][$stones[$i][1]] = $stones[$i][2];
77        }
78        $tInfo = [];
79        $tInfo[0] = $highestX;
80        $tInfo[1] = $highestY;
81        $arr = [];
82        $arr[0] = $board;
83        $arr[1] = $stones;
84        $arr[2] = $tInfo;
85        $arr[3] = $boardSize;
86
87        return $arr;
88    }
89
90    public static function xFlip($stones) {
91        $stonesCount = count($stones);
92        for ($i = 0; $i < $stonesCount; $i++) {
93            $stones[$i][0] = 18 - $stones[$i][0];
94        }
95
96        return $stones;
97    }
98
99    public static function yFlip($stones) {
100        $stonesCount = count($stones);
101        for ($i = 0; $i < $stonesCount; $i++) {
102            $stones[$i][1] = 18 - $stones[$i][1];
103        }
104
105        return $stones;
106    }
107
108    public static function getInitialPositionEnd($pos, $sgfArr) {
109        $endCondition = 0;
110        $currentPos1 = $pos + 2;
111        $currentPos2 = $pos + 5;
112        while ($sgfArr[$currentPos1] == '[' && $sgfArr[$currentPos2] == ']') {
113            $endCondition = $currentPos2;
114            $currentPos1 += 4;
115            $currentPos2 += 4;
116        }
117
118        return $endCondition;
119    }
120
121    public static function getInitialPosition($pos, $sgfArr, $color) {
122        $arr = [];
123        $end = AppController::getInitialPositionEnd($pos, $sgfArr);
124        for ($i = $pos + 2; $i < $end; $i++) {
125            if ($sgfArr[$i] != '[' && $sgfArr[$i] != ']') {
126                array_push($arr, strtolower($sgfArr[$i]));
127            }
128        }
129        $alphabet = array_flip(['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z']);
130        $xy = true;
131        $arr2 = [];
132        $c = 0;
133        $arrCount = count($arr);
134        for ($i = 0; $i < $arrCount; $i++) {
135            $arr[$i] = $alphabet[$arr[$i]];
136            if ($xy) {
137                $arr2[$c] = [];
138                $arr2[$c][0] = $arr[$i];
139            } else {
140                $arr2[$c][1] = $arr[$i];
141                $arr2[$c][2] = $color;
142                $c++;
143            }
144            $xy = !$xy;
145        }
146
147        return $arr2;
148    }
149
150    protected function getDeletedSets() {
151        $dSets = [];
152        $de = $this->Set->find('all', ['conditions' => ['public' => -1]]);
153        if (!$de) {
154            $de = [];
155        }
156        foreach ($de as $item) {
157            $dSets[] = $item['Set']['id'];
158        }
159
160        return $dSets;
161    }
162
163    /**
164     * @return void
165     */
166    public static function startPageUpdate() {
167        $str = '';
168        $latest = ClassRegistry::init('AchievementStatus')->find('all', ['limit' => 7, 'order' => 'created DESC']);
169        if (!$latest) {
170            $latest = [];
171        }
172        $latestCount = count($latest);
173        for ($i = 0; $i < $latestCount; $i++) {
174            $a = ClassRegistry::init('Achievement')->findById($latest[$i]['AchievementStatus']['achievement_id']);
175            $u = ClassRegistry::init('User')->findById($latest[$i]['AchievementStatus']['user_id']);
176            if (substr($u['User']['name'], 0, 3) == 'g__' && $u['User']['external_id'] != null) {
177                $startPageUser = AppController::checkPicture($u);
178            } else {
179                $startPageUser = $u['User']['name'];
180            }
181            $latest[$i]['AchievementStatus']['name'] = $a['Achievement']['name'];
182            $latest[$i]['AchievementStatus']['color'] = $a['Achievement']['color'];
183            $latest[$i]['AchievementStatus']['image'] = $a['Achievement']['image'];
184            $latest[$i]['AchievementStatus']['user'] = $startPageUser;
185            $str .= '<div class="quote1"><div class="quote1a"><a href="/achievements/view/' . $a['Achievement']['id'] . '"><img src="/img/' . $a['Achievement']['image'] . '.png" width="34px"></a></div>';
186            $str .= '<div class="quote1b">Achievement gained by ' . $startPageUser . ':<br><div class=""><b>' . $a['Achievement']['name'] . '</b></div></div></div>';
187        }
188        file_put_contents('mainPageAjax.txt', $str);
189    }
190
191    protected function saveSolvedNumber($uid) {
192        $this->loadModel('User');
193        $this->loadModel('TsumegoStatus');
194        $this->loadModel('Set');
195        $this->loadModel('SetConnection');
196
197        $solvedUts2 = 0;
198        $tsumegos = $this->SetConnection->find('all');
199        if (!$tsumegos) {
200            $tsumegos = [];
201        }
202        $uts = $this->TsumegoStatus->find('all', ['order' => 'updated DESC', 'conditions' => ['user_id' => $uid]]);
203        if (!$uts) {
204            $uts = [];
205        }
206        $setKeys = [];
207        $setArray = $this->Set->find('all', ['conditions' => ['public' => 1]]);
208        if (!$setArray) {
209            $setArray = [];
210        }
211
212        $setArrayCount = count($setArray);
213        for ($i = 0; $i < $setArrayCount; $i++) {
214            $setKeys[$setArray[$i]['Set']['id']] = $setArray[$i]['Set']['id'];
215        }
216
217        $scs = [];
218        $tsumegosCount = count($tsumegos);
219        for ($j = 0; $j < $tsumegosCount; $j++) {
220            if (!isset($scs[$tsumegos[$j]['SetConnection']['tsumego_id']])) {
221                $scs[$tsumegos[$j]['SetConnection']['tsumego_id']] = 1;
222            } else {
223                $scs[$tsumegos[$j]['SetConnection']['tsumego_id']]++;
224            }
225        }
226        $utsCount = count($uts);
227        for ($j = 0; $j < $utsCount; $j++) {
228            if ($uts[$j]['TsumegoStatus']['status'] == 'S' || $uts[$j]['TsumegoStatus']['status'] == 'W' || $uts[$j]['TsumegoStatus']['status'] == 'C') {
229                if (isset($scs[$uts[$j]['TsumegoStatus']['tsumego_id']])) {
230                    $solvedUts2 += $scs[$uts[$j]['TsumegoStatus']['tsumego_id']];
231                }
232            }
233        }
234        Auth::getUser()['solved'] = $solvedUts2;
235        Auth::saveUser();
236
237        return $solvedUts2;
238    }
239
240    /**
241     * @return void
242     */
243    protected function resetUserElos() {
244        $this->loadModel('User');
245
246        $u = $this->User->find('all', [
247            'conditions' => [
248                'id >=' => 15000,
249                'id <=' => 19000,
250            ],
251        ]);
252        if (!$u) {
253            $u = [];
254        }
255
256        $uCount = count($u);
257        for ($i = 0; $i < $uCount; $i++) {
258            $u[$i]['User']['rating'] = 900;
259            $u[$i]['User']['solved2'] = 0;
260            $this->User->save($u[$i]);
261        }
262    }
263
264    /**
265     * @param int $uid User ID
266     * @param string $action Action type
267     *
268     * @return void
269     */
270    public static function handleContribution($uid, $action) {
271        $uc = ClassRegistry::init('UserContribution')->find('first', ['conditions' => ['user_id' => $uid]]);
272        if ($uc == null) {
273            $uc = [];
274            $uc['UserContribution']['user_id'] = $uid;
275            $uc['UserContribution']['added_tag'] = 0;
276            $uc['UserContribution']['created_tag'] = 0;
277            $uc['UserContribution']['made_proposal'] = 0;
278            $uc['UserContribution']['reviewed'] = 0;
279            $uc['UserContribution']['score'] = 0;
280            ClassRegistry::init('UserContribution')->create();
281        }
282        $uc['UserContribution'][$action] += 1;
283        $uc['UserContribution']['score']
284        = $uc['UserContribution']['added_tag']
285        + $uc['UserContribution']['created_tag'] * 3
286        + $uc['UserContribution']['made_proposal'] * 5
287        + $uc['UserContribution']['reviewed'] * 2;
288        ClassRegistry::init('UserContribution')->save($uc);
289    }
290
291    public static function getAllTags($not) {
292        $a = [];
293        $notApproved = ClassRegistry::init('TagName')->find('all', ['conditions' => ['approved' => 0]]);
294        if (!$notApproved) {
295            $notApproved = [];
296        }
297        $notCount = count($not);
298        for ($i = 0; $i < $notCount; $i++) {
299            array_push($a, $not[$i]['Tag']['tag_name_id']);
300        }
301        $notApprovedCount = count($notApproved);
302        for ($i = 0; $i < $notApprovedCount; $i++) {
303            array_push($a, $notApproved[$i]['TagName']['id']);
304        }
305        $tn = ClassRegistry::init('TagName')->find('all', [
306            'conditions' => [
307                'NOT' => ['id' => $a],
308            ],
309        ]);
310        if (!$tn) {
311            $tn = [];
312        }
313        $sorted = [];
314        $keys = [];
315        $tnCount = count($tn);
316        for ($i = 0; $i < $tnCount; $i++) {
317            array_push($sorted, $tn[$i]['TagName']['name']);
318            $keys[$tn[$i]['TagName']['name']] = $tn[$i];
319        }
320        sort($sorted);
321        $s2 = [];
322        $sortedCount = count($sorted);
323        for ($i = 0; $i < $sortedCount; $i++) {
324            array_push($s2, $keys[$sorted[$i]]);
325        }
326
327        return $s2;
328    }
329
330    public static function encrypt($str = null) {
331        $secret_key = 'my_simple_secret_keyx';
332        $secret_iv = 'my_simple_secret_ivx';
333        $encrypt_method = 'AES-256-CBC';
334        $key = hash('sha256', $secret_key);
335        $iv = substr(hash('sha256', $secret_iv), 0, 16);
336
337        return base64_encode(openssl_encrypt($str, $encrypt_method, $key, 0, $iv));
338    }
339
340    protected function checkPictureLarge($u) {
341        if (substr($u['User']['name'], 0, 3) == 'g__' && $u['User']['external_id'] != null) {
342            return '<img class="google-profile-image-large" src="/img/google/' . $u['User']['picture'] . '">' . substr($u['User']['name'], 3);
343        }
344
345        return $u['User']['name'];
346    }
347    public static function checkPicture($user) {
348        if (substr($user['name'], 0, 3) == 'g__' && $user['external_id'] != null) {
349            return '<img class="google-profile-image" src="/img/google/' . $user['picture'] . '">' . substr($user['name'], 3);
350        }
351
352        return $user['name'];
353    }
354
355    public static function saveDanSolveCondition($solvedTsumegoRank, $tId): void {
356        if ($solvedTsumegoRank == '1d' || $solvedTsumegoRank == '2d' || $solvedTsumegoRank == '3d' || $solvedTsumegoRank == '4d' || $solvedTsumegoRank == '5d') {
357            $danSolveCategory = 'danSolve' . $solvedTsumegoRank;
358            $danSolveCondition = ClassRegistry::init('AchievementCondition')->find('first', [
359                'order' => 'value DESC',
360                'conditions' => [
361                    'user_id' => Auth::getUserID(),
362                    'category' => $danSolveCategory,
363                ],
364            ]);
365            if (!$danSolveCondition) {
366                $danSolveCondition = [];
367                $danSolveCondition['AchievementCondition']['value'] = 0;
368                ClassRegistry::init('AchievementCondition')->create();
369            }
370            $danSolveCondition['AchievementCondition']['category'] = $danSolveCategory;
371            $danSolveCondition['AchievementCondition']['user_id'] = Auth::getUserID();
372            $danSolveCondition['AchievementCondition']['set_id'] = $tId;
373            $danSolveCondition['AchievementCondition']['value']++;
374
375            ClassRegistry::init('AchievementCondition')->save($danSolveCondition);
376        }
377    }
378
379    public static function updateSprintCondition(bool $trigger = false): void {
380        if (Auth::isLoggedIn()) {
381            $sprintCondition = ClassRegistry::init('AchievementCondition')->find('first', [
382                'order' => 'value DESC',
383                'conditions' => [
384                    'user_id' => Auth::getUserID(),
385                    'category' => 'sprint',
386                ],
387            ]);
388            if (!$sprintCondition) {
389                $sprintCondition = [];
390                $sprintCondition['AchievementCondition']['value'] = 0;
391                ClassRegistry::init('AchievementCondition')->create();
392            }
393            $sprintCondition['AchievementCondition']['category'] = 'sprint';
394            $sprintCondition['AchievementCondition']['user_id'] = Auth::getUserID();
395            if ($trigger) {
396                $sprintCondition['AchievementCondition']['value']++;
397            } else {
398                $sprintCondition['AchievementCondition']['value'] = 0;
399            }
400            ClassRegistry::init('AchievementCondition')->save($sprintCondition);
401        }
402    }
403
404    public static function updateGoldenCondition(bool $trigger = false): void {
405        $goldenCondition = ClassRegistry::init('AchievementCondition')->find('first', [
406            'order' => 'value DESC',
407            'conditions' => [
408                'user_id' => Auth::getUserID(),
409                'category' => 'golden',
410            ],
411        ]);
412        if (!$goldenCondition) {
413            $goldenCondition = [];
414            $goldenCondition['AchievementCondition']['value'] = 0;
415            ClassRegistry::init('AchievementCondition')->create();
416        }
417        $goldenCondition['AchievementCondition']['category'] = 'golden';
418        $goldenCondition['AchievementCondition']['user_id'] = Auth::getUserID();
419        if ($trigger) {
420            $goldenCondition['AchievementCondition']['value']++;
421        } else {
422            $goldenCondition['AchievementCondition']['value'] = 0;
423        }
424        ClassRegistry::init('AchievementCondition')->save($goldenCondition);
425    }
426
427    public static function setPotionCondition(): void {
428        $potionCondition = ClassRegistry::init('AchievementCondition')->find('first', [
429            'order' => 'value DESC',
430            'conditions' => [
431                'user_id' => Auth::getUserID(),
432                'category' => 'potion',
433            ],
434        ]);
435        if (!$potionCondition) {
436            $potionCondition = [];
437            ClassRegistry::init('AchievementCondition')->create();
438        }
439        $potionCondition['AchievementCondition']['category'] = 'potion';
440        $potionCondition['AchievementCondition']['user_id'] = Auth::getUserID();
441        $potionCondition['AchievementCondition']['value'] = 1;
442        ClassRegistry::init('AchievementCondition')->save($potionCondition);
443    }
444
445    public static function updateGems(string $rank): void {
446        $datex = new DateTime('today');
447        $dateGem = ClassRegistry::init('DayRecord')->find('first', ['conditions' => ['date' => $datex->format('Y-m-d')]]);
448        if ($dateGem != null) {
449            $gems = explode('-', $dateGem['DayRecord']['gems']);
450            $gemValue = '';
451            $gemValue2 = '';
452            $gemValue3 = '';
453            $condition1 = 500;
454            $condition2 = 200;
455            $condition3 = 5;
456            $found1 = false;
457            $found2 = false;
458            $found3 = false;
459            if ($rank == '15k' || $rank == '14k' || $rank == '13k' || $rank == '12k' || $rank == '11k' || $rank == '10k') {
460                if ($gems[0] == 0) {
461                    $gemValue = '15k';
462                } elseif ($gems[0] == 1) {
463                    $gemValue = '12k';
464                } elseif ($gems[0] == 2) {
465                    $gemValue = '10k';
466                }
467                if ($rank == $gemValue) {
468                    $dateGem['DayRecord']['gemCounter1']++;
469                    if ($dateGem['DayRecord']['gemCounter1'] == $condition1) {
470                        $found1 = true;
471                    }
472                }
473            } elseif ($rank == '9k' || $rank == '8k' || $rank == '7k' || $rank == '6k' || $rank == '5k' || $rank == '4k' || $rank == '3k' || $rank == '2k' || $rank == '1k') {
474                if ($gems[1] == 0) {
475                    $gemValue = '9k';
476                    $gemValue2 = 'x';
477                    $gemValue3 = 'y';
478                } elseif ($gems[1] == 1) {
479                    $gemValue = '6k';
480                    $gemValue2 = '5k';
481                    $gemValue3 = '4k';
482                } elseif ($gems[1] == 2) {
483                    $gemValue = 'x';
484                    $gemValue2 = '2k';
485                    $gemValue3 = '1k';
486                }
487                if ($rank == $gemValue || $rank == $gemValue2 || $rank == $gemValue3) {
488                    $dateGem['DayRecord']['gemCounter2']++;
489                    if ($dateGem['DayRecord']['gemCounter2'] == $condition2) {
490                        $found2 = true;
491                    }
492                }
493            } elseif ($rank == '1d' || $rank == '2d' || $rank == '3d' || $rank == '4d' || $rank == '5d' || $rank == '6d' || $rank == '7d') {
494                if ($gems[2] == 0) {
495                    $gemValue = '1d';
496                    $gemValue2 = '2d';
497                    $gemValue3 = '3d';
498                } elseif ($gems[2] == 1) {
499                    $gemValue = '2d';
500                    $gemValue2 = '3d';
501                    $gemValue3 = '4d';
502                } elseif ($gems[2] == 2) {
503                    $gemValue = '5d';
504                    $gemValue2 = '6d';
505                    $gemValue3 = '7d';
506                }
507                if ($rank == $gemValue || $rank == $gemValue2 || $rank == $gemValue3) {
508                    $dateGem['DayRecord']['gemCounter3']++;
509                    if ($dateGem['DayRecord']['gemCounter3'] == $condition3) {
510                        $found3 = true;
511                    }
512                }
513            }
514            if ($found1) {
515                $aCondition = ClassRegistry::init('AchievementCondition')->find('first', [
516                    'order' => 'value DESC',
517                    'conditions' => [
518                        'user_id' => Auth::getUserID(),
519                        'category' => 'emerald',
520                    ],
521                ]);
522                if ($aCondition == null) {
523                    $aCondition = [];
524                    $aCondition['AchievementCondition']['category'] = 'emerald';
525                    $aCondition['AchievementCondition']['user_id'] = Auth::getUserID();
526                    $aCondition['AchievementCondition']['value'] = 1;
527                    ClassRegistry::init('AchievementCondition')->save($aCondition);
528                } else {
529                    $dateGem['DayRecord']['gemCounter1']--;
530                }
531            } elseif ($found2) {
532                $aCondition = ClassRegistry::init('AchievementCondition')->find('first', [
533                    'order' => 'value DESC',
534                    'conditions' => [
535                        'user_id' => Auth::getUserID(),
536                        'category' => 'sapphire',
537                    ],
538                ]);
539                if ($aCondition == null) {
540                    $aCondition = [];
541                    $aCondition['AchievementCondition']['category'] = 'sapphire';
542                    $aCondition['AchievementCondition']['user_id'] = Auth::getUserID();
543                    $aCondition['AchievementCondition']['value'] = 1;
544                    ClassRegistry::init('AchievementCondition')->save($aCondition);
545                } else {
546                    $dateGem['DayRecord']['gemCounter2']--;
547                }
548            } elseif ($found3) {
549                $aCondition = ClassRegistry::init('AchievementCondition')->find('first', [
550                    'order' => 'value DESC',
551                    'conditions' => [
552                        'user_id' => Auth::getUserID(),
553                        'category' => 'ruby',
554                    ],
555                ]);
556                if ($aCondition == null) {
557                    $aCondition = [];
558                    $aCondition['AchievementCondition']['category'] = 'ruby';
559                    $aCondition['AchievementCondition']['user_id'] = Auth::getUserID();
560                    $aCondition['AchievementCondition']['value'] = 1;
561                    ClassRegistry::init('AchievementCondition')->save($aCondition);
562                } else {
563                    $dateGem['DayRecord']['gemCounter3']--;
564                }
565            }
566        }
567        ClassRegistry::init('DayRecord')->save($dateGem);
568    }
569
570    public static function checkProblemNumberAchievements() {
571        if (!Auth::isLoggedIn()) {
572            return;
573        }
574
575        $buffer = ClassRegistry::init('AchievementStatus')->find('all', ['conditions' => ['user_id' => Auth::getUserID()]]);
576        if (!$buffer) {
577            $buffer = [];
578        }
579        $existingAs = [];
580        $bufferCount = count($buffer);
581        for ($i = 0; $i < $bufferCount; $i++) {
582            $existingAs[$buffer[$i]['AchievementStatus']['achievement_id']] = $buffer[$i];
583        }
584        $as = [];
585        $as['AchievementStatus']['user_id'] = Auth::getUserID();
586        $updated = [];
587
588        $achievementId = 1;
589        $solvedCount = Auth::getUser()['solved'];
590        if ($solvedCount >= 1000 && !isset($existingAs[$achievementId])) {
591            $as['AchievementStatus']['achievement_id'] = $achievementId;
592            ClassRegistry::init('AchievementStatus')->create();
593            ClassRegistry::init('AchievementStatus')->save($as);
594            array_push($updated, $achievementId);
595        }
596        $achievementId = 2;
597        if ($solvedCount >= 2000 && !isset($existingAs[$achievementId])) {
598            $as['AchievementStatus']['achievement_id'] = $achievementId;
599            ClassRegistry::init('AchievementStatus')->create();
600            ClassRegistry::init('AchievementStatus')->save($as);
601            array_push($updated, $achievementId);
602        }
603        $achievementId = 3;
604        if ($solvedCount >= 3000 && !isset($existingAs[$achievementId])) {
605            $as['AchievementStatus']['achievement_id'] = $achievementId;
606            ClassRegistry::init('AchievementStatus')->create();
607            ClassRegistry::init('AchievementStatus')->save($as);
608            array_push($updated, $achievementId);
609        }
610        $achievementId = 4;
611        if ($solvedCount >= 4000 && !isset($existingAs[$achievementId])) {
612            $as['AchievementStatus']['achievement_id'] = $achievementId;
613            ClassRegistry::init('AchievementStatus')->create();
614            ClassRegistry::init('AchievementStatus')->save($as);
615            array_push($updated, $achievementId);
616        }
617        $achievementId = 5;
618        if ($solvedCount >= 5000 && !isset($existingAs[$achievementId])) {
619            $as['AchievementStatus']['achievement_id'] = $achievementId;
620            ClassRegistry::init('AchievementStatus')->create();
621            ClassRegistry::init('AchievementStatus')->save($as);
622            array_push($updated, $achievementId);
623        }
624        $achievementId = 6;
625        if ($solvedCount >= 6000 && !isset($existingAs[$achievementId])) {
626            $as['AchievementStatus']['achievement_id'] = $achievementId;
627            ClassRegistry::init('AchievementStatus')->create();
628            ClassRegistry::init('AchievementStatus')->save($as);
629            array_push($updated, $achievementId);
630        }
631        $achievementId = 7;
632        if ($solvedCount >= 7000 && !isset($existingAs[$achievementId])) {
633            $as['AchievementStatus']['achievement_id'] = $achievementId;
634            ClassRegistry::init('AchievementStatus')->create();
635            ClassRegistry::init('AchievementStatus')->save($as);
636            array_push($updated, $achievementId);
637        }
638        $achievementId = 8;
639        if ($solvedCount >= 8000 && !isset($existingAs[$achievementId])) {
640            $as['AchievementStatus']['achievement_id'] = $achievementId;
641            ClassRegistry::init('AchievementStatus')->create();
642            ClassRegistry::init('AchievementStatus')->save($as);
643            array_push($updated, $achievementId);
644        }
645        $achievementId = 9;
646        if ($solvedCount >= 9000 && !isset($existingAs[$achievementId])) {
647            $as['AchievementStatus']['achievement_id'] = $achievementId;
648            ClassRegistry::init('AchievementStatus')->create();
649            ClassRegistry::init('AchievementStatus')->save($as);
650            array_push($updated, $achievementId);
651        }
652        $achievementId = 10;
653        if ($solvedCount >= 10000 && !isset($existingAs[$achievementId])) {
654            $as['AchievementStatus']['achievement_id'] = $achievementId;
655            ClassRegistry::init('AchievementStatus')->create();
656            ClassRegistry::init('AchievementStatus')->save($as);
657            array_push($updated, $achievementId);
658        }
659        //uotd achievement
660        $achievementId = 11;
661        if (!isset($existingAs[$achievementId])) {
662            $condition = ClassRegistry::init('AchievementCondition')->find('first', ['conditions' => ['user_id' => Auth::getUserID(), 'category' => 'uotd']]);
663            if ($condition != null) {
664                $as['AchievementStatus']['achievement_id'] = $achievementId;
665                ClassRegistry::init('AchievementStatus')->create();
666                ClassRegistry::init('AchievementStatus')->save($as);
667                array_push($updated, $achievementId);
668            }
669        }
670
671        $updatedCount = count($updated);
672        for ($i = 0; $i < $updatedCount; $i++) {
673            $a = ClassRegistry::init('Achievement')->findById($updated[$i]);
674            $updated[$i] = [];
675            $updated[$i][0] = $a['Achievement']['name'];
676            $updated[$i][1] = $a['Achievement']['description'];
677            $updated[$i][2] = $a['Achievement']['image'];
678            $updated[$i][3] = $a['Achievement']['color'];
679            $updated[$i][4] = $a['Achievement']['xp'];
680            $updated[$i][5] = $a['Achievement']['id'];
681        }
682
683        return $updated;
684    }
685
686    public static function checkDanSolveAchievements() {
687        if (Auth::isLoggedIn()) {
688            $buffer = ClassRegistry::init('AchievementStatus')->find('all', ['conditions' => ['user_id' => Auth::getUserID()]]);
689            if (!$buffer) {
690                $buffer = [];
691            }
692            $ac = ClassRegistry::init('AchievementCondition')->find('all', [
693                'order' => 'category ASC',
694                'conditions' => [
695                    'user_id' => Auth::getUserID(),
696                    'OR' => [
697                        ['category' => 'danSolve1d'],
698                        ['category' => 'danSolve2d'],
699                        ['category' => 'danSolve3d'],
700                        ['category' => 'danSolve4d'],
701                        ['category' => 'danSolve5d'],
702                        ['category' => 'emerald'],
703                        ['category' => 'sapphire'],
704                        ['category' => 'ruby'],
705                        ['category' => 'sprint'],
706                        ['category' => 'golden'],
707                        ['category' => 'potion'],
708                    ],
709                ],
710            ]);
711            if (!$ac) {
712                $ac = [];
713            }
714            $ac1 = [];
715            $acCount = count($ac);
716            for ($i = 0; $i < $acCount; $i++) {
717                if ($ac[$i]['AchievementCondition']['category'] == 'danSolve1d') {
718                    $ac1['1d'] = $ac[$i]['AchievementCondition']['value'];
719                } elseif ($ac[$i]['AchievementCondition']['category'] == 'danSolve2d') {
720                    $ac1['2d'] = $ac[$i]['AchievementCondition']['value'];
721                } elseif ($ac[$i]['AchievementCondition']['category'] == 'danSolve3d') {
722                    $ac1['3d'] = $ac[$i]['AchievementCondition']['value'];
723                } elseif ($ac[$i]['AchievementCondition']['category'] == 'danSolve4d') {
724                    $ac1['4d'] = $ac[$i]['AchievementCondition']['value'];
725                } elseif ($ac[$i]['AchievementCondition']['category'] == 'danSolve5d') {
726                    $ac1['5d'] = $ac[$i]['AchievementCondition']['value'];
727                } elseif ($ac[$i]['AchievementCondition']['category'] == 'emerald') {
728                    $ac1['emerald'] = $ac[$i]['AchievementCondition']['value'];
729                } elseif ($ac[$i]['AchievementCondition']['category'] == 'sapphire') {
730                    $ac1['sapphire'] = $ac[$i]['AchievementCondition']['value'];
731                } elseif ($ac[$i]['AchievementCondition']['category'] == 'ruby') {
732                    $ac1['ruby'] = $ac[$i]['AchievementCondition']['value'];
733                } elseif ($ac[$i]['AchievementCondition']['category'] == 'sprint') {
734                    $ac1['sprint'] = $ac[$i]['AchievementCondition']['value'];
735                } elseif ($ac[$i]['AchievementCondition']['category'] == 'golden') {
736                    $ac1['golden'] = $ac[$i]['AchievementCondition']['value'];
737                } elseif ($ac[$i]['AchievementCondition']['category'] == 'potion') {
738                    $ac1['potion'] = $ac[$i]['AchievementCondition']['value'];
739                }
740            }
741
742            $existingAs = [];
743            $bufferCount = count($buffer);
744            for ($i = 0; $i < $bufferCount; $i++) {
745                $existingAs[$buffer[$i]['AchievementStatus']['achievement_id']] = $buffer[$i];
746            }
747            $as = [];
748            $as['AchievementStatus']['user_id'] = Auth::getUserID();
749            $updated = [];
750            $achievementId = 101;
751            if ($ac1['1d'] > 0 && !isset($existingAs[$achievementId])) {
752                $as['AchievementStatus']['achievement_id'] = $achievementId;
753                ClassRegistry::init('AchievementStatus')->create();
754                ClassRegistry::init('AchievementStatus')->save($as);
755                array_push($updated, $achievementId);
756            }
757            $achievementId = 102;
758            if ($ac1['2d'] > 0 && !isset($existingAs[$achievementId])) {
759                $as['AchievementStatus']['achievement_id'] = $achievementId;
760                ClassRegistry::init('AchievementStatus')->create();
761                ClassRegistry::init('AchievementStatus')->save($as);
762                array_push($updated, $achievementId);
763            }
764            $achievementId = 103;
765            if ($ac1['3d'] > 0 && !isset($existingAs[$achievementId])) {
766                $as['AchievementStatus']['achievement_id'] = $achievementId;
767                ClassRegistry::init('AchievementStatus')->create();
768                ClassRegistry::init('AchievementStatus')->save($as);
769                array_push($updated, $achievementId);
770            }
771            $achievementId = 104;
772            if ($ac1['4d'] > 0 && !isset($existingAs[$achievementId])) {
773                $as['AchievementStatus']['achievement_id'] = $achievementId;
774                ClassRegistry::init('AchievementStatus')->create();
775                ClassRegistry::init('AchievementStatus')->save($as);
776                array_push($updated, $achievementId);
777            }
778            $achievementId = 105;
779            if ($ac1['5d'] > 0 && !isset($existingAs[$achievementId])) {
780                $as['AchievementStatus']['achievement_id'] = $achievementId;
781                ClassRegistry::init('AchievementStatus')->create();
782                ClassRegistry::init('AchievementStatus')->save($as);
783                array_push($updated, $achievementId);
784            }
785            $achievementId = 106;
786            if ($ac1['1d'] >= 10 && !isset($existingAs[$achievementId])) {
787                $as['AchievementStatus']['achievement_id'] = $achievementId;
788                ClassRegistry::init('AchievementStatus')->create();
789                ClassRegistry::init('AchievementStatus')->save($as);
790                array_push($updated, $achievementId);
791            }
792            $achievementId = 107;
793            if ($ac1['2d'] >= 10 && !isset($existingAs[$achievementId])) {
794                $as['AchievementStatus']['achievement_id'] = $achievementId;
795                ClassRegistry::init('AchievementStatus')->create();
796                ClassRegistry::init('AchievementStatus')->save($as);
797                array_push($updated, $achievementId);
798            }
799            $achievementId = 108;
800            if ($ac1['3d'] >= 10 && !isset($existingAs[$achievementId])) {
801                $as['AchievementStatus']['achievement_id'] = $achievementId;
802                ClassRegistry::init('AchievementStatus')->create();
803                ClassRegistry::init('AchievementStatus')->save($as);
804                array_push($updated, $achievementId);
805            }
806            $achievementId = 109;
807            if ($ac1['4d'] >= 10 && !isset($existingAs[$achievementId])) {
808                $as['AchievementStatus']['achievement_id'] = $achievementId;
809                ClassRegistry::init('AchievementStatus')->create();
810                ClassRegistry::init('AchievementStatus')->save($as);
811                array_push($updated, $achievementId);
812            }
813            $achievementId = 110;
814            if ($ac1['5d'] >= 10 && !isset($existingAs[$achievementId])) {
815                $as['AchievementStatus']['achievement_id'] = $achievementId;
816                ClassRegistry::init('AchievementStatus')->create();
817                ClassRegistry::init('AchievementStatus')->save($as);
818                array_push($updated, $achievementId);
819            }
820            $achievementId = 111;
821            if (isset($ac1['emerald'])) {
822                if ($ac1['emerald'] == 1 && !isset($existingAs[$achievementId])) {
823                    $as['AchievementStatus']['achievement_id'] = $achievementId;
824                    ClassRegistry::init('AchievementStatus')->create();
825                    ClassRegistry::init('AchievementStatus')->save($as);
826                    array_push($updated, $achievementId);
827                }
828            }
829            $achievementId = 112;
830            if (isset($ac1['sapphire'])) {
831                if ($ac1['sapphire'] == 1 && !isset($existingAs[$achievementId])) {
832                    $as['AchievementStatus']['achievement_id'] = $achievementId;
833                    ClassRegistry::init('AchievementStatus')->create();
834                    ClassRegistry::init('AchievementStatus')->save($as);
835                    array_push($updated, $achievementId);
836                }
837            }
838            $achievementId = 113;
839            if (isset($ac1['ruby'])) {
840                if ($ac1['ruby'] == 1 && !isset($existingAs[$achievementId])) {
841                    $as['AchievementStatus']['achievement_id'] = $achievementId;
842                    ClassRegistry::init('AchievementStatus')->create();
843                    ClassRegistry::init('AchievementStatus')->save($as);
844                    array_push($updated, $achievementId);
845                }
846            }
847            $achievementId = 114;
848            if (!isset($existingAs[$achievementId]) && isset($existingAs[111]) && isset($existingAs[112]) && isset($existingAs[113])) {
849                $as['AchievementStatus']['achievement_id'] = $achievementId;
850                ClassRegistry::init('AchievementStatus')->create();
851                ClassRegistry::init('AchievementStatus')->save($as);
852                array_push($updated, $achievementId);
853            }
854            $achievementId = 96;
855            if (!isset($existingAs[$achievementId]) && $ac1['sprint'] >= 30) {
856                $as['AchievementStatus']['achievement_id'] = $achievementId;
857                ClassRegistry::init('AchievementStatus')->create();
858                ClassRegistry::init('AchievementStatus')->save($as);
859                array_push($updated, $achievementId);
860            }
861            $achievementId = 97;
862            if (!isset($existingAs[$achievementId]) && $ac1['golden'] >= 10) {
863                $as['AchievementStatus']['achievement_id'] = $achievementId;
864                ClassRegistry::init('AchievementStatus')->create();
865                ClassRegistry::init('AchievementStatus')->save($as);
866                array_push($updated, $achievementId);
867            }
868            $achievementId = 98;
869            if (!isset($existingAs[$achievementId]) && $ac1['potion'] >= 1) {
870                $as['AchievementStatus']['achievement_id'] = $achievementId;
871                ClassRegistry::init('AchievementStatus')->create();
872                ClassRegistry::init('AchievementStatus')->save($as);
873                array_push($updated, $achievementId);
874            }
875            $updatedCount = count($updated);
876            for ($i = 0; $i < $updatedCount; $i++) {
877                $a = ClassRegistry::init('Achievement')->findById($updated[$i]);
878                $updated[$i] = [];
879                $updated[$i][0] = $a['Achievement']['name'];
880                $updated[$i][1] = $a['Achievement']['description'];
881                $updated[$i][2] = $a['Achievement']['image'];
882                $updated[$i][3] = $a['Achievement']['color'];
883                $updated[$i][4] = $a['Achievement']['xp'];
884                $updated[$i][5] = $a['Achievement']['id'];
885            }
886
887            return $updated;
888        }
889    }
890
891    protected function checkForLocked($t, $setsWithPremium) {
892        $scCheck = $this->SetConnection->find('first', ['conditions' => ['tsumego_id' => $t['Tsumego']['id']]]);
893        if ($scCheck && in_array($scCheck['SetConnection']['set_id'], $setsWithPremium) && !Auth::hasPremium()) {
894            $t['Tsumego']['locked'] = true;
895        } else {
896            $t['Tsumego']['locked'] = false;
897        }
898
899        return $t;
900    }
901    public static function checkNoErrorAchievements() {
902        if (Auth::isLoggedIn()) {
903
904            $ac = ClassRegistry::init('AchievementCondition')->find('first', [
905                'order' => 'value DESC',
906                'conditions' => [
907                    'user_id' => Auth::getUserID(),
908                    'category' => 'err',
909                ],
910            ]);
911
912            $buffer = ClassRegistry::init('AchievementStatus')->find('all', ['conditions' => ['user_id' => Auth::getUserID()]]);
913            if (!$buffer) {
914                $buffer = [];
915            }
916            $existingAs = [];
917            $bufferCount = count($buffer);
918            for ($i = 0; $i < $bufferCount; $i++) {
919                $existingAs[$buffer[$i]['AchievementStatus']['achievement_id']] = $buffer[$i];
920            }
921            $as = [];
922            $as['AchievementStatus']['user_id'] = Auth::getUserID();
923            $updated = [];
924
925            $achievementId = 53;
926            if ($ac['AchievementCondition']['value'] >= 10 && !isset($existingAs[$achievementId])) {
927                $as['AchievementStatus']['achievement_id'] = $achievementId;
928                ClassRegistry::init('AchievementStatus')->create();
929                ClassRegistry::init('AchievementStatus')->save($as);
930                array_push($updated, $achievementId);
931            }
932            $achievementId = 54;
933            if ($ac['AchievementCondition']['value'] >= 20 && !isset($existingAs[$achievementId])) {
934                $as['AchievementStatus']['achievement_id'] = $achievementId;
935                ClassRegistry::init('AchievementStatus')->create();
936                ClassRegistry::init('AchievementStatus')->save($as);
937                array_push($updated, $achievementId);
938            }
939            $achievementId = 55;
940            if ($ac['AchievementCondition']['value'] >= 30 && !isset($existingAs[$achievementId])) {
941                $as['AchievementStatus']['achievement_id'] = $achievementId;
942                ClassRegistry::init('AchievementStatus')->create();
943                ClassRegistry::init('AchievementStatus')->save($as);
944                array_push($updated, $achievementId);
945            }
946            $achievementId = 56;
947            if ($ac['AchievementCondition']['value'] >= 50 && !isset($existingAs[$achievementId])) {
948                $as['AchievementStatus']['achievement_id'] = $achievementId;
949                ClassRegistry::init('AchievementStatus')->create();
950                ClassRegistry::init('AchievementStatus')->save($as);
951                array_push($updated, $achievementId);
952            }
953            $achievementId = 57;
954            if ($ac['AchievementCondition']['value'] >= 100 && !isset($existingAs[$achievementId])) {
955                $as['AchievementStatus']['achievement_id'] = $achievementId;
956                ClassRegistry::init('AchievementStatus')->create();
957                ClassRegistry::init('AchievementStatus')->save($as);
958                array_push($updated, $achievementId);
959            }
960            $achievementId = 58;
961            if ($ac['AchievementCondition']['value'] >= 200 && !isset($existingAs[$achievementId])) {
962                $as['AchievementStatus']['achievement_id'] = $achievementId;
963                ClassRegistry::init('AchievementStatus')->create();
964                ClassRegistry::init('AchievementStatus')->save($as);
965                array_push($updated, $achievementId);
966            }
967            $updatedCount = count($updated);
968            for ($i = 0; $i < $updatedCount; $i++) {
969                $a = ClassRegistry::init('Achievement')->findById($updated[$i]);
970                $updated[$i] = [];
971                $updated[$i][0] = $a['Achievement']['name'];
972                $updated[$i][1] = $a['Achievement']['description'];
973                $updated[$i][2] = $a['Achievement']['image'];
974                $updated[$i][3] = $a['Achievement']['color'];
975                $updated[$i][4] = $a['Achievement']['xp'];
976                $updated[$i][5] = $a['Achievement']['id'];
977            }
978
979            return $updated;
980        }
981    }
982
983    protected function checkTimeModeAchievements() {
984        $this->loadModel('Achievement');
985        $this->loadModel('AchievementStatus');
986        $this->loadModel('TimeModeSession');
987
988        $buffer = ClassRegistry::init('AchievementStatus')->find('all', ['conditions' => ['user_id' => Auth::getUserID()]]);
989        if (!$buffer) {
990            $buffer = [];
991        }
992        $existingAs = [];
993        $bufferCount = count($buffer);
994        for ($i = 0; $i < $bufferCount; $i++) {
995            $existingAs[$buffer[$i]['AchievementStatus']['achievement_id']] = $buffer[$i];
996        }
997        $as = [];
998        $as['AchievementStatus']['user_id'] = Auth::getUserID();
999        $updated = [];
1000
1001        $rBlitz = $this->TimeModeSession->find('all', ['conditions' => ['time_mode_category_id' => TimeModeUtil::$CATEGORY_BLITZ, 'user_id' => Auth::getUserID()]]);
1002        if (!$rBlitz) {
1003            $rBlitz = [];
1004        }
1005        $rFast = $this->TimeModeSession->find('all', ['conditions' => ['time_mode_category_id' => TimeModeUtil::$CATEGORY_FAST_SPEED, 'user_id' => Auth::getUserID()]]);
1006        if (!$rFast) {
1007            $rFast = [];
1008        }
1009        $rSlow = $this->TimeModeSession->find('all', ['conditions' => ['time_mode_category_id' => TimeModeUtil::$CATEGORY_SLOW_SPEED, 'user_id' => Auth::getUserID()]]);
1010        if (!$rSlow) {
1011            $rSlow = [];
1012        }
1013        $r = $this->TimeModeSession->find('all', ['conditions' => ['user_id' => Auth::getUserID()]]);
1014        if (!$r) {
1015            $r = [];
1016        }
1017
1018        $timeModeAchievements = [];
1019        for ($i = 70; $i <= 91; $i++) {
1020            $timeModeAchievements[$i] = false;
1021        }
1022        $rCount = count($r);
1023        for ($i = 0; $i < $rCount; $i++) {
1024            if ($r[$i]['TimeModeSession']['status'] == 's') {
1025                if ($r[$i]['TimeModeSession']['TimeModeAttempt'] == '5k') {
1026                    if ($r[$i]['TimeModeSession']['mode'] == 2) {
1027                        $timeModeAchievements[70] = true;
1028                    } elseif ($r[$i]['TimeModeSession']['mode'] == 1) {
1029                        $timeModeAchievements[76] = true;
1030                    } elseif ($r[$i]['TimeModeSession']['mode'] == 0) {
1031                        $timeModeAchievements[82] = true;
1032                    }
1033                } elseif ($r[$i]['TimeModeSession']['TimeModeAttempt'] == '4k') {
1034                    if ($r[$i]['TimeModeSession']['mode'] == 2) {
1035                        $timeModeAchievements[71] = true;
1036                    } elseif ($r[$i]['TimeModeSession']['mode'] == 1) {
1037                        $timeModeAchievements[77] = true;
1038                    } elseif ($r[$i]['TimeModeSession']['mode'] == 0) {
1039                        $timeModeAchievements[83] = true;
1040                    }
1041                } elseif ($r[$i]['TimeModeSession']['TimeModeAttempt'] == '3k') {
1042                    if ($r[$i]['TimeModeSession']['mode'] == 2) {
1043                        $timeModeAchievements[72] = true;
1044                    } elseif ($r[$i]['TimeModeSession']['mode'] == 1) {
1045                        $timeModeAchievements[78] = true;
1046                    } elseif ($r[$i]['TimeModeSession']['mode'] == 0) {
1047                        $timeModeAchievements[84] = true;
1048                    }
1049                } elseif ($r[$i]['TimeModeSession']['TimeModeAttempt'] == '2k') {
1050                    if ($r[$i]['TimeModeSession']['mode'] == 2) {
1051                        $timeModeAchievements[73] = true;
1052                    } elseif ($r[$i]['TimeModeSession']['mode'] == 1) {
1053                        $timeModeAchievements[79] = true;
1054                    } elseif ($r[$i]['TimeModeSession']['mode'] == 0) {
1055                        $timeModeAchievements[85] = true;
1056                    }
1057                } elseif ($r[$i]['TimeModeSession']['TimeModeAttempt'] == '1k') {
1058                    if ($r[$i]['TimeModeSession']['mode'] == 2) {
1059                        $timeModeAchievements[74] = true;
1060                    } elseif ($r[$i]['TimeModeSession']['mode'] == 1) {
1061                        $timeModeAchievements[80] = true;
1062                    } elseif ($r[$i]['TimeModeSession']['mode'] == 0) {
1063                        $timeModeAchievements[86] = true;
1064                    }
1065                } elseif ($r[$i]['TimeModeSession']['TimeModeAttempt'] == '1d') {
1066                    if ($r[$i]['TimeModeSession']['mode'] == 2) {
1067                        $timeModeAchievements[75] = true;
1068                    } elseif ($r[$i]['TimeModeSession']['mode'] == 1) {
1069                        $timeModeAchievements[81] = true;
1070                    } elseif ($r[$i]['TimeModeSession']['mode'] == 0) {
1071                        $timeModeAchievements[87] = true;
1072                    }
1073                }
1074            }
1075            if ($r[$i]['TimeModeSession']['points'] >= 850
1076            && ($r[$i]['TimeModeSession']['TimeModeAttempt'] == '4k' || $r[$i]['TimeModeSession']['TimeModeAttempt'] == '3k' || $r[$i]['TimeModeSession']['TimeModeAttempt'] == '2k' || $r[$i]['TimeModeSession']['TimeModeAttempt'] == '1k'
1077            || $r[$i]['TimeModeSession']['TimeModeAttempt'] == '1d' || $r[$i]['TimeModeSession']['TimeModeAttempt'] == '2d' || $r[$i]['TimeModeSession']['TimeModeAttempt'] == '3d' || $r[$i]['TimeModeSession']['TimeModeAttempt'] == '4d'
1078            || $r[$i]['TimeModeSession']['TimeModeAttempt'] == '5d')) {
1079                $timeModeAchievements[91] = true;
1080            }
1081            if ($r[$i]['TimeModeSession']['points'] >= 875
1082            && ($r[$i]['TimeModeSession']['TimeModeAttempt'] == '4k' || $r[$i]['TimeModeSession']['TimeModeAttempt'] == '3k' || $r[$i]['TimeModeSession']['TimeModeAttempt'] == '2k' || $r[$i]['TimeModeSession']['TimeModeAttempt'] == '1k'
1083            || $r[$i]['TimeModeSession']['TimeModeAttempt'] == '1d' || $r[$i]['TimeModeSession']['TimeModeAttempt'] == '2d' || $r[$i]['TimeModeSession']['TimeModeAttempt'] == '3d' || $r[$i]['TimeModeSession']['TimeModeAttempt'] == '4d'
1084            || $r[$i]['TimeModeSession']['TimeModeAttempt'] == '5d' || $r[$i]['TimeModeSession']['TimeModeAttempt'] == '5k' || $r[$i]['TimeModeSession']['TimeModeAttempt'] == '6k')) {
1085                $timeModeAchievements[90] = true;
1086            }
1087            if ($r[$i]['TimeModeSession']['points'] >= 900
1088            && ($r[$i]['TimeModeSession']['TimeModeAttempt'] == '4k' || $r[$i]['TimeModeSession']['TimeModeAttempt'] == '3k' || $r[$i]['TimeModeSession']['TimeModeAttempt'] == '2k' || $r[$i]['TimeModeSession']['TimeModeAttempt'] == '1k'
1089            || $r[$i]['TimeModeSession']['TimeModeAttempt'] == '1d' || $r[$i]['TimeModeSession']['TimeModeAttempt'] == '2d' || $r[$i]['TimeModeSession']['TimeModeAttempt'] == '3d' || $r[$i]['TimeModeSession']['TimeModeAttempt'] == '4d'
1090            || $r[$i]['TimeModeSession']['TimeModeAttempt'] == '5d' || $r[$i]['TimeModeSession']['TimeModeAttempt'] == '5k' || $r[$i]['TimeModeSession']['TimeModeAttempt'] == '6k' || $r[$i]['TimeModeSession']['TimeModeAttempt'] == '7k'
1091            || $r[$i]['TimeModeSession']['TimeModeAttempt'] == '8k')) {
1092                $timeModeAchievements[89] = true;
1093            }
1094            if ($r[$i]['TimeModeSession']['points'] >= 950
1095            && ($r[$i]['TimeModeSession']['TimeModeAttempt'] == '4k' || $r[$i]['TimeModeSession']['TimeModeAttempt'] == '3k' || $r[$i]['TimeModeSession']['TimeModeAttempt'] == '2k' || $r[$i]['TimeModeSession']['TimeModeAttempt'] == '1k'
1096            || $r[$i]['TimeModeSession']['TimeModeAttempt'] == '1d' || $r[$i]['TimeModeSession']['TimeModeAttempt'] == '2d' || $r[$i]['TimeModeSession']['TimeModeAttempt'] == '3d' || $r[$i]['TimeModeSession']['TimeModeAttempt'] == '4d'
1097            || $r[$i]['TimeModeSession']['TimeModeAttempt'] == '5d' || $r[$i]['TimeModeSession']['TimeModeAttempt'] == '5k' || $r[$i]['TimeModeSession']['TimeModeAttempt'] == '6k' || $r[$i]['TimeModeSession']['TimeModeAttempt'] == '7k'
1098            || $r[$i]['TimeModeSession']['TimeModeAttempt'] == '8k' || $r[$i]['TimeModeSession']['TimeModeAttempt'] == '9k' || $r[$i]['TimeModeSession']['TimeModeAttempt'] == '10k')) {
1099                $timeModeAchievements[88] = true;
1100            }
1101        }
1102        for ($i = 70; $i <= 91; $i++) {
1103            $achievementId = $i;
1104            if ($timeModeAchievements[$achievementId] == true && !isset($existingAs[$achievementId])) {
1105                $as['AchievementStatus']['achievement_id'] = $achievementId;
1106                ClassRegistry::init('AchievementStatus')->create();
1107                ClassRegistry::init('AchievementStatus')->save($as);
1108                array_push($updated, $achievementId);
1109            }
1110        }
1111        $updatedCount = count($updated);
1112        for ($i = 0; $i < $updatedCount; $i++) {
1113            $a = ClassRegistry::init('Achievement')->findById($updated[$i]);
1114            $updated[$i] = [];
1115            $updated[$i][0] = $a['Achievement']['name'];
1116            $updated[$i][1] = $a['Achievement']['description'];
1117            $updated[$i][2] = $a['Achievement']['image'];
1118            $updated[$i][3] = $a['Achievement']['color'];
1119            $updated[$i][4] = $a['Achievement']['xp'];
1120            $updated[$i][5] = $a['Achievement']['id'];
1121        }
1122
1123        return $updated;
1124    }
1125
1126    public static function checkRatingAchievements() {
1127        if (!Auth::isLoggedIn()) {
1128            return;
1129        }
1130
1131        $buffer = ClassRegistry::init('AchievementStatus')->find('all', ['conditions' => ['user_id' => Auth::getUserID()]]);
1132        if (!$buffer) {
1133            $buffer = [];
1134        }
1135        $existingAs = [];
1136        $bufferCount = count($buffer);
1137        for ($i = 0; $i < $bufferCount; $i++) {
1138            $existingAs[$buffer[$i]['AchievementStatus']['achievement_id']] = $buffer[$i];
1139        }
1140        $as = [];
1141        $as['AchievementStatus']['user_id'] = Auth::getUserID();
1142        $updated = [];
1143
1144        $achievementId = 59;
1145        $currentElo = Auth::getUser()['rating'];
1146        if ($currentElo >= 1500 && !isset($existingAs[$achievementId])) {
1147            $as['AchievementStatus']['achievement_id'] = $achievementId;
1148            ClassRegistry::init('AchievementStatus')->create();
1149            ClassRegistry::init('AchievementStatus')->save($as);
1150            array_push($updated, $achievementId);
1151        }
1152        $achievementId = 60;
1153        if ($currentElo >= 1600 && !isset($existingAs[$achievementId])) {
1154            $as['AchievementStatus']['achievement_id'] = $achievementId;
1155            ClassRegistry::init('AchievementStatus')->create();
1156            ClassRegistry::init('AchievementStatus')->save($as);
1157            array_push($updated, $achievementId);
1158        }
1159        $achievementId = 61;
1160        if ($currentElo >= 1700 && !isset($existingAs[$achievementId])) {
1161            $as['AchievementStatus']['achievement_id'] = $achievementId;
1162            ClassRegistry::init('AchievementStatus')->create();
1163            ClassRegistry::init('AchievementStatus')->save($as);
1164            array_push($updated, $achievementId);
1165        }
1166        $achievementId = 62;
1167        if ($currentElo >= 1800 && !isset($existingAs[$achievementId])) {
1168            $as['AchievementStatus']['achievement_id'] = $achievementId;
1169            ClassRegistry::init('AchievementStatus')->create();
1170            ClassRegistry::init('AchievementStatus')->save($as);
1171            array_push($updated, $achievementId);
1172        }
1173        $achievementId = 63;
1174        if ($currentElo >= 1900 && !isset($existingAs[$achievementId])) {
1175            $as['AchievementStatus']['achievement_id'] = $achievementId;
1176            ClassRegistry::init('AchievementStatus')->create();
1177            ClassRegistry::init('AchievementStatus')->save($as);
1178            array_push($updated, $achievementId);
1179        }
1180        $achievementId = 64;
1181        if ($currentElo >= 2000 && !isset($existingAs[$achievementId])) {
1182            $as['AchievementStatus']['achievement_id'] = $achievementId;
1183            ClassRegistry::init('AchievementStatus')->create();
1184            ClassRegistry::init('AchievementStatus')->save($as);
1185            array_push($updated, $achievementId);
1186        }
1187        $achievementId = 65;
1188        if ($currentElo >= 2100 && !isset($existingAs[$achievementId])) {
1189            $as['AchievementStatus']['achievement_id'] = $achievementId;
1190            ClassRegistry::init('AchievementStatus')->create();
1191            ClassRegistry::init('AchievementStatus')->save($as);
1192            array_push($updated, $achievementId);
1193        }
1194        $achievementId = 66;
1195        if ($currentElo >= 2200 && !isset($existingAs[$achievementId])) {
1196            $as['AchievementStatus']['achievement_id'] = $achievementId;
1197            ClassRegistry::init('AchievementStatus')->create();
1198            ClassRegistry::init('AchievementStatus')->save($as);
1199            array_push($updated, $achievementId);
1200        }
1201        $achievementId = 67;
1202        if ($currentElo >= 2300 && !isset($existingAs[$achievementId])) {
1203            $as['AchievementStatus']['achievement_id'] = $achievementId;
1204            ClassRegistry::init('AchievementStatus')->create();
1205            ClassRegistry::init('AchievementStatus')->save($as);
1206            array_push($updated, $achievementId);
1207        }
1208        $achievementId = 68;
1209        if ($currentElo >= 2400 && !isset($existingAs[$achievementId])) {
1210            $as['AchievementStatus']['achievement_id'] = $achievementId;
1211            ClassRegistry::init('AchievementStatus')->create();
1212            ClassRegistry::init('AchievementStatus')->save($as);
1213            array_push($updated, $achievementId);
1214        }
1215        $achievementId = 69;
1216        if ($currentElo >= 2500 && !isset($existingAs[$achievementId])) {
1217            $as['AchievementStatus']['achievement_id'] = $achievementId;
1218            ClassRegistry::init('AchievementStatus')->create();
1219            ClassRegistry::init('AchievementStatus')->save($as);
1220            array_push($updated, $achievementId);
1221        }
1222        $updatedCount = count($updated);
1223        for ($i = 0; $i < $updatedCount; $i++) {
1224            $a = ClassRegistry::init('Achievement')->findById($updated[$i]);
1225            $updated[$i] = [];
1226            $updated[$i][0] = $a['Achievement']['name'];
1227            $updated[$i][1] = $a['Achievement']['description'];
1228            $updated[$i][2] = $a['Achievement']['image'];
1229            $updated[$i][3] = $a['Achievement']['color'];
1230            $updated[$i][4] = $a['Achievement']['xp'];
1231            $updated[$i][5] = $a['Achievement']['id'];
1232        }
1233
1234        return $updated;
1235    }
1236
1237    public static function checkLevelAchievements() {
1238        if (!Auth::isLoggedIn()) {
1239            return;
1240        }
1241        $buffer = ClassRegistry::init('AchievementStatus')->find('all', ['conditions' => ['user_id' => Auth::getUserID()]]);
1242        if (!$buffer) {
1243            $buffer = [];
1244        }
1245        $existingAs = [];
1246        $bufferCount = count($buffer);
1247        for ($i = 0; $i < $bufferCount; $i++) {
1248            $existingAs[$buffer[$i]['AchievementStatus']['achievement_id']] = $buffer[$i];
1249        }
1250        $as = [];
1251        $as['AchievementStatus']['user_id'] = Auth::getUserID();
1252        $updated = [];
1253
1254        $achievementId = 36;
1255        $userLevel = Auth::getUser()['level'];
1256        if ($userLevel >= 10 && !isset($existingAs[$achievementId])) {
1257            $as['AchievementStatus']['achievement_id'] = $achievementId;
1258            ClassRegistry::init('AchievementStatus')->create();
1259            ClassRegistry::init('AchievementStatus')->save($as);
1260            array_push($updated, $achievementId);
1261        }
1262        $achievementId = 37;
1263        if ($userLevel >= 20 && !isset($existingAs[$achievementId])) {
1264            $as['AchievementStatus']['achievement_id'] = $achievementId;
1265            ClassRegistry::init('AchievementStatus')->create();
1266            ClassRegistry::init('AchievementStatus')->save($as);
1267            array_push($updated, $achievementId);
1268        }
1269        $achievementId = 38;
1270        if ($userLevel >= 30 && !isset($existingAs[$achievementId])) {
1271            $as['AchievementStatus']['achievement_id'] = $achievementId;
1272            ClassRegistry::init('AchievementStatus')->create();
1273            ClassRegistry::init('AchievementStatus')->save($as);
1274            array_push($updated, $achievementId);
1275        }
1276        $achievementId = 39;
1277        if ($userLevel >= 40 && !isset($existingAs[$achievementId])) {
1278            $as['AchievementStatus']['achievement_id'] = $achievementId;
1279            ClassRegistry::init('AchievementStatus')->create();
1280            ClassRegistry::init('AchievementStatus')->save($as);
1281            array_push($updated, $achievementId);
1282        }
1283        $achievementId = 40;
1284        if ($userLevel >= 50 && !isset($existingAs[$achievementId])) {
1285            $as['AchievementStatus']['achievement_id'] = $achievementId;
1286            ClassRegistry::init('AchievementStatus')->create();
1287            ClassRegistry::init('AchievementStatus')->save($as);
1288            array_push($updated, $achievementId);
1289        }
1290        $achievementId = 41;
1291        if ($userLevel >= 60 && !isset($existingAs[$achievementId])) {
1292            $as['AchievementStatus']['achievement_id'] = $achievementId;
1293            ClassRegistry::init('AchievementStatus')->create();
1294            ClassRegistry::init('AchievementStatus')->save($as);
1295            array_push($updated, $achievementId);
1296        }
1297        $achievementId = 42;
1298        if ($userLevel >= 70 && !isset($existingAs[$achievementId])) {
1299            $as['AchievementStatus']['achievement_id'] = $achievementId;
1300            ClassRegistry::init('AchievementStatus')->create();
1301            ClassRegistry::init('AchievementStatus')->save($as);
1302            array_push($updated, $achievementId);
1303        }
1304        $achievementId = 43;
1305        if ($userLevel >= 80 && !isset($existingAs[$achievementId])) {
1306            $as['AchievementStatus']['achievement_id'] = $achievementId;
1307            ClassRegistry::init('AchievementStatus')->create();
1308            ClassRegistry::init('AchievementStatus')->save($as);
1309            array_push($updated, $achievementId);
1310        }
1311        $achievementId = 44;
1312        if ($userLevel >= 90 && !isset($existingAs[$achievementId])) {
1313            $as['AchievementStatus']['achievement_id'] = $achievementId;
1314            ClassRegistry::init('AchievementStatus')->create();
1315            ClassRegistry::init('AchievementStatus')->save($as);
1316            array_push($updated, $achievementId);
1317        }
1318        $achievementId = 45;
1319        if ($userLevel >= 100 && !isset($existingAs[$achievementId])) {
1320            $as['AchievementStatus']['achievement_id'] = $achievementId;
1321            ClassRegistry::init('AchievementStatus')->create();
1322            ClassRegistry::init('AchievementStatus')->save($as);
1323            array_push($updated, $achievementId);
1324        }
1325        $achievementId = 100;
1326        if (Auth::hasPremium() && !isset($existingAs[$achievementId])) {
1327            $as['AchievementStatus']['achievement_id'] = $achievementId;
1328            ClassRegistry::init('AchievementStatus')->create();
1329            ClassRegistry::init('AchievementStatus')->save($as);
1330            array_push($updated, $achievementId);
1331        }
1332        $updatedCount = count($updated);
1333        for ($i = 0; $i < $updatedCount; $i++) {
1334            $a = ClassRegistry::init('Achievement')->findById($updated[$i]);
1335            $updated[$i] = [];
1336            $updated[$i][0] = $a['Achievement']['name'];
1337            $updated[$i][1] = $a['Achievement']['description'];
1338            $updated[$i][2] = $a['Achievement']['image'];
1339            $updated[$i][3] = $a['Achievement']['color'];
1340            $updated[$i][4] = $a['Achievement']['xp'];
1341            $updated[$i][5] = $a['Achievement']['id'];
1342        }
1343
1344        return $updated;
1345    }
1346
1347    protected function checkSetCompletedAchievements() {
1348        $this->loadModel('Set');
1349        $this->loadModel('Tsumego');
1350        $this->loadModel('Achievement');
1351        $this->loadModel('AchievementStatus');
1352        $this->loadModel('AchievementCondition');
1353
1354        $ac = ClassRegistry::init('AchievementCondition')->find('first', [
1355            'order' => 'value DESC',
1356            'conditions' => [
1357                'user_id' => Auth::getUserID(),
1358                'category' => 'set',
1359            ],
1360        ]);
1361
1362        if (!$ac) {
1363            return [];
1364        }
1365
1366        $buffer = ClassRegistry::init('AchievementStatus')->find('all', ['conditions' => ['user_id' => Auth::getUserID()]]);
1367        if (!$buffer) {
1368            $buffer = [];
1369        }
1370        $existingAs = [];
1371        $bufferCount = count($buffer);
1372        for ($i = 0; $i < $bufferCount; $i++) {
1373            $existingAs[$buffer[$i]['AchievementStatus']['achievement_id']] = $buffer[$i];
1374        }
1375        $as = [];
1376        $as['AchievementStatus']['user_id'] = Auth::getUserID();
1377        $updated = [];
1378
1379        $achievementId = 47;
1380        if ($ac['AchievementCondition']['value'] >= 10 && !isset($existingAs[$achievementId])) {
1381            $as['AchievementStatus']['achievement_id'] = $achievementId;
1382            ClassRegistry::init('AchievementStatus')->create();
1383            ClassRegistry::init('AchievementStatus')->save($as);
1384            array_push($updated, $achievementId);
1385        }
1386        $achievementId = 48;
1387        if ($ac['AchievementCondition']['value'] >= 20 && !isset($existingAs[$achievementId])) {
1388            $as['AchievementStatus']['achievement_id'] = $achievementId;
1389            ClassRegistry::init('AchievementStatus')->create();
1390            ClassRegistry::init('AchievementStatus')->save($as);
1391            array_push($updated, $achievementId);
1392        }
1393        $achievementId = 49;
1394        if ($ac['AchievementCondition']['value'] >= 30 && !isset($existingAs[$achievementId])) {
1395            $as['AchievementStatus']['achievement_id'] = $achievementId;
1396            ClassRegistry::init('AchievementStatus')->create();
1397            ClassRegistry::init('AchievementStatus')->save($as);
1398            array_push($updated, $achievementId);
1399        }
1400        $achievementId = 50;
1401        if ($ac['AchievementCondition']['value'] >= 40 && !isset($existingAs[$achievementId])) {
1402            $as['AchievementStatus']['achievement_id'] = $achievementId;
1403            ClassRegistry::init('AchievementStatus')->create();
1404            ClassRegistry::init('AchievementStatus')->save($as);
1405            array_push($updated, $achievementId);
1406        }
1407        $achievementId = 51;
1408        if ($ac['AchievementCondition']['value'] >= 50 && !isset($existingAs[$achievementId])) {
1409            $as['AchievementStatus']['achievement_id'] = $achievementId;
1410            ClassRegistry::init('AchievementStatus')->create();
1411            ClassRegistry::init('AchievementStatus')->save($as);
1412            array_push($updated, $achievementId);
1413        }
1414        $achievementId = 52;
1415        if ($ac['AchievementCondition']['value'] >= 60 && !isset($existingAs[$achievementId])) {
1416            $as['AchievementStatus']['achievement_id'] = $achievementId;
1417            ClassRegistry::init('AchievementStatus')->create();
1418            ClassRegistry::init('AchievementStatus')->save($as);
1419            array_push($updated, $achievementId);
1420        }
1421        $updatedCount = count($updated);
1422        for ($i = 0; $i < $updatedCount; $i++) {
1423            $a = ClassRegistry::init('Achievement')->findById($updated[$i]);
1424            $updated[$i] = [];
1425            $updated[$i][0] = $a['Achievement']['name'];
1426            $updated[$i][1] = $a['Achievement']['description'];
1427            $updated[$i][2] = $a['Achievement']['image'];
1428            $updated[$i][3] = $a['Achievement']['color'];
1429            $updated[$i][4] = $a['Achievement']['xp'];
1430            $updated[$i][5] = $a['Achievement']['id'];
1431        }
1432
1433        return $updated;
1434    }
1435
1436    protected function setAchievementSpecial($s = null) {
1437        $this->loadModel('Set');
1438        $this->loadModel('Tsumego');
1439        $this->loadModel('TsumegoStatus');
1440        $this->loadModel('Achievement');
1441        $this->loadModel('AchievementStatus');
1442        $this->loadModel('SetConnection');
1443
1444        $buffer = ClassRegistry::init('AchievementStatus')->find('all', ['conditions' => ['user_id' => Auth::getUserID()]]);
1445        if (!$buffer) {
1446            $buffer = [];
1447        }
1448        $existingAs = [];
1449        $bufferCount = count($buffer);
1450        for ($i = 0; $i < $bufferCount; $i++) {
1451            $existingAs[$buffer[$i]['AchievementStatus']['achievement_id']] = $buffer[$i];
1452        }
1453        $as = [];
1454        $as['AchievementStatus']['user_id'] = Auth::getUserID();
1455        $updated = [];
1456
1457        $tsIds = [];
1458        $completed = '';
1459        if ($s == 'cc1') {
1460            $ts1 = TsumegoUtil::collectTsumegosFromSet(50);
1461            $ts2 = TsumegoUtil::collectTsumegosFromSet(52);
1462            $ts3 = TsumegoUtil::collectTsumegosFromSet(53);
1463            $ts4 = TsumegoUtil::collectTsumegosFromSet(54);
1464            $ts = array_merge($ts1, $ts2, $ts3, $ts4);
1465            $tsCount = count($ts);
1466            for ($i = 0; $i < $tsCount; $i++) {
1467                array_push($tsIds, $ts[$i]['Tsumego']['id']);
1468            }
1469            $uts = $this->TsumegoStatus->find('all', [
1470                'conditions' => [
1471                    'user_id' => Auth::getUserID(),
1472                    'tsumego_id' => $tsIds,
1473                ],
1474            ]);
1475            if (!$uts) {
1476                $uts = [];
1477            }
1478            $counter = 0;
1479            $utsCount = count($uts);
1480            for ($j = 0; $j < $utsCount; $j++) {
1481                for ($k = 0; $k < $tsCount; $k++) {
1482                    if ($uts[$j]['TsumegoStatus']['tsumego_id'] == $ts[$k]['Tsumego']['id'] && ($uts[$j]['TsumegoStatus']['status'] == 'S'
1483                    || $uts[$j]['TsumegoStatus']['status'] == 'W' || $uts[$j]['TsumegoStatus']['status'] == 'C')) {
1484                        $counter++;
1485                    }
1486                }
1487            }
1488            if ($counter == count($ts)) {
1489                $completed = $s;
1490            }
1491        } elseif ($s == 'cc2') {
1492            $ts1 = TsumegoUtil::collectTsumegosFromSet(41);
1493            $ts2 = TsumegoUtil::collectTsumegosFromSet(49);
1494            $ts3 = TsumegoUtil::collectTsumegosFromSet(65);
1495            $ts4 = TsumegoUtil::collectTsumegosFromSet(66);
1496            $ts = array_merge($ts1, $ts2, $ts3, $ts4);
1497            $tsCount = count($ts);
1498            for ($i = 0; $i < $tsCount; $i++) {
1499                array_push($tsIds, $ts[$i]['Tsumego']['id']);
1500            }
1501            $uts = $this->TsumegoStatus->find('all', [
1502                'conditions' => [
1503                    'user_id' => Auth::getUserID(),
1504                    'tsumego_id' => $tsIds,
1505                ],
1506            ]);
1507            if (!$uts) {
1508                $uts = [];
1509            }
1510            $counter = 0;
1511            $utsCount = count($uts);
1512            for ($j = 0; $j < $utsCount; $j++) {
1513                for ($k = 0; $k < $tsCount; $k++) {
1514                    if ($uts[$j]['TsumegoStatus']['tsumego_id'] == $ts[$k]['Tsumego']['id'] && ($uts[$j]['TsumegoStatus']['status'] == 'S'
1515                    || $uts[$j]['TsumegoStatus']['status'] == 'W' || $uts[$j]['TsumegoStatus']['status'] == 'C')) {
1516                        $counter++;
1517                    }
1518                }
1519            }
1520            if ($counter == count($ts)) {
1521                $completed = $s;
1522            }
1523        } elseif ($s == 'cc3') {
1524            $ts1 = TsumegoUtil::collectTsumegosFromSet(186);
1525            $ts2 = TsumegoUtil::collectTsumegosFromSet(187);
1526            $ts3 = TsumegoUtil::collectTsumegosFromSet(196);
1527            $ts4 = TsumegoUtil::collectTsumegosFromSet(203);
1528            $ts = array_merge($ts1, $ts2, $ts3, $ts4);
1529            $tsCount = count($ts);
1530            for ($i = 0; $i < $tsCount; $i++) {
1531                array_push($tsIds, $ts[$i]['Tsumego']['id']);
1532            }
1533            $uts = $this->TsumegoStatus->find('all', [
1534                'conditions' => [
1535                    'user_id' => Auth::getUserID(),
1536                    'tsumego_id' => $tsIds,
1537                ],
1538            ]);
1539            if (!$uts) {
1540                $uts = [];
1541            }
1542            $counter = 0;
1543            $utsCount = count($uts);
1544            for ($j = 0; $j < $utsCount; $j++) {
1545                for ($k = 0; $k < $tsCount; $k++) {
1546                    if ($uts[$j]['TsumegoStatus']['tsumego_id'] == $ts[$k]['Tsumego']['id'] && ($uts[$j]['TsumegoStatus']['status'] == 'S'
1547                    || $uts[$j]['TsumegoStatus']['status'] == 'W' || $uts[$j]['TsumegoStatus']['status'] == 'C')) {
1548                        $counter++;
1549                    }
1550                }
1551            }
1552            if ($counter == count($ts)) {
1553                $completed = $s;
1554            }
1555        } elseif ($s == '1000w1') {
1556            $ts1 = TsumegoUtil::collectTsumegosFromSet(190);
1557            $ts2 = TsumegoUtil::collectTsumegosFromSet(193);
1558            $ts3 = TsumegoUtil::collectTsumegosFromSet(198);
1559            $ts = array_merge($ts1, $ts2, $ts3);
1560            $tsCount = count($ts);
1561            for ($i = 0; $i < $tsCount; $i++) {
1562                array_push($tsIds, $ts[$i]['Tsumego']['id']);
1563            }
1564            $uts = $this->TsumegoStatus->find('all', [
1565                'conditions' => [
1566                    'user_id' => Auth::getUserID(),
1567                    'tsumego_id' => $tsIds,
1568                ],
1569            ]);
1570            if (!$uts) {
1571                $uts = [];
1572            }
1573            $counter = 0;
1574            $utsCount = count($uts);
1575            for ($j = 0; $j < $utsCount; $j++) {
1576                for ($k = 0; $k < $tsCount; $k++) {
1577                    if ($uts[$j]['TsumegoStatus']['tsumego_id'] == $ts[$k]['Tsumego']['id'] && ($uts[$j]['TsumegoStatus']['status'] == 'S'
1578                    || $uts[$j]['TsumegoStatus']['status'] == 'W' || $uts[$j]['TsumegoStatus']['status'] == 'C')) {
1579                        $counter++;
1580                    }
1581                }
1582            }
1583            if ($counter == count($ts)) {
1584                $completed = $s;
1585            }
1586        } elseif ($s == '1000w2') {
1587            $ts = TsumegoUtil::collectTsumegosFromSet(216);
1588            $tsCount = count($ts);
1589            for ($i = 0; $i < $tsCount; $i++) {
1590                array_push($tsIds, $ts[$i]['Tsumego']['id']);
1591            }
1592            $uts = $this->TsumegoStatus->find('all', [
1593                'conditions' => [
1594                    'user_id' => Auth::getUserID(),
1595                    'tsumego_id' => $tsIds,
1596                ],
1597            ]);
1598            if (!$uts) {
1599                $uts = [];
1600            }
1601            $counter = 0;
1602            $utsCount = count($uts);
1603            for ($j = 0; $j < $utsCount; $j++) {
1604                for ($k = 0; $k < $tsCount; $k++) {
1605                    if ($uts[$j]['TsumegoStatus']['tsumego_id'] == $ts[$k]['Tsumego']['id'] && ($uts[$j]['TsumegoStatus']['status'] == 'S'
1606                    || $uts[$j]['TsumegoStatus']['status'] == 'W' || $uts[$j]['TsumegoStatus']['status'] == 'C')) {
1607                        $counter++;
1608                    }
1609                }
1610            }
1611            if ($counter == count($ts)) {
1612                $completed = $s;
1613            }
1614        }
1615
1616        $achievementId = 92;
1617        if ($completed == 'cc1' && !isset($existingAs[$achievementId])) {
1618            $as['AchievementStatus']['achievement_id'] = $achievementId;
1619            ClassRegistry::init('AchievementStatus')->create();
1620            ClassRegistry::init('AchievementStatus')->save($as);
1621            array_push($updated, $achievementId);
1622        }
1623        $achievementId = 93;
1624        if ($completed == 'cc2' && !isset($existingAs[$achievementId])) {
1625            $as['AchievementStatus']['achievement_id'] = $achievementId;
1626            ClassRegistry::init('AchievementStatus')->create();
1627            ClassRegistry::init('AchievementStatus')->save($as);
1628            array_push($updated, $achievementId);
1629        }
1630        $achievementId = 94;
1631        if ($completed == 'cc3' && !isset($existingAs[$achievementId])) {
1632            $as['AchievementStatus']['achievement_id'] = $achievementId;
1633            ClassRegistry::init('AchievementStatus')->create();
1634            ClassRegistry::init('AchievementStatus')->save($as);
1635            array_push($updated, $achievementId);
1636        }
1637        $achievementId = 95;
1638        if ($completed == '1000w1' && !isset($existingAs[$achievementId])) {
1639            $as['AchievementStatus']['achievement_id'] = $achievementId;
1640            ClassRegistry::init('AchievementStatus')->create();
1641            ClassRegistry::init('AchievementStatus')->save($as);
1642            array_push($updated, $achievementId);
1643        }
1644        $achievementId = 115;
1645        if ($completed == '1000w2' && !isset($existingAs[$achievementId])) {
1646            $as['AchievementStatus']['achievement_id'] = $achievementId;
1647            ClassRegistry::init('AchievementStatus')->create();
1648            ClassRegistry::init('AchievementStatus')->save($as);
1649            array_push($updated, $achievementId);
1650        }
1651        $updatedCount = count($updated);
1652        for ($i = 0; $i < $updatedCount; $i++) {
1653            $a = ClassRegistry::init('Achievement')->findById($updated[$i]);
1654            $updated[$i] = [];
1655            $updated[$i][0] = $a['Achievement']['name'];
1656            $updated[$i][1] = $a['Achievement']['description'];
1657            $updated[$i][2] = $a['Achievement']['image'];
1658            $updated[$i][3] = $a['Achievement']['color'];
1659            $updated[$i][4] = $a['Achievement']['xp'];
1660            $updated[$i][5] = $a['Achievement']['id'];
1661        }
1662
1663        return $updated;
1664    }
1665
1666    protected function checkSetAchievements($sid = null) {
1667        $this->loadModel('Set');
1668        $this->loadModel('Tsumego');
1669        $this->loadModel('Achievement');
1670        $this->loadModel('AchievementStatus');
1671        $this->loadModel('AchievementCondition');
1672
1673        //$tNum = count($this->Tsumego->find('all', array('conditions' => array('set_id' => $sid))));
1674        $tNum = count(TsumegoUtil::collectTsumegosFromSet($sid));
1675        $s = $this->Set->findById($sid);
1676        $acA = ClassRegistry::init('AchievementCondition')->find('first', [
1677            'order' => 'value DESC',
1678            'conditions' => [
1679                'set_id' => $sid,
1680                'user_id' => Auth::getUserID(),
1681                'category' => '%',
1682            ],
1683        ]);
1684        if (!$acA) {
1685            return [];
1686        }
1687        $acS = ClassRegistry::init('AchievementCondition')->find('first', [
1688            'order' => 'value ASC',
1689            'conditions' => [
1690                'set_id' => $sid,
1691                'user_id' => Auth::getUserID(),
1692                'category' => 's',
1693            ],
1694        ]);
1695        $buffer = ClassRegistry::init('AchievementStatus')->find('all', ['conditions' => ['user_id' => Auth::getUserID()]]);
1696        if (!$buffer) {
1697            $buffer = [];
1698        }
1699        $existingAs = [];
1700        $bufferCount = count($buffer);
1701        for ($i = 0; $i < $bufferCount; $i++) {
1702            $existingAs[$buffer[$i]['AchievementStatus']['achievement_id']] = $buffer[$i];
1703        }
1704        $as = [];
1705        $as['AchievementStatus']['user_id'] = Auth::getUserID();
1706        $updated = [];
1707
1708        $achievementId = 99;
1709        if ($sid == -1 && !isset($existingAs[$achievementId])) {
1710            $as['AchievementStatus']['achievement_id'] = $achievementId;
1711            ClassRegistry::init('AchievementStatus')->create();
1712            ClassRegistry::init('AchievementStatus')->save($as);
1713            array_push($updated, $achievementId);
1714        }
1715        if ($tNum >= 100) {
1716            if ($s['Set']['difficulty'] < 1300) {
1717                $achievementId = 12;
1718                if ($acA['AchievementCondition']['value'] >= 75 && !isset($existingAs[$achievementId])) {
1719                    $as['AchievementStatus']['achievement_id'] = $achievementId;
1720                    ClassRegistry::init('AchievementStatus')->create();
1721                    ClassRegistry::init('AchievementStatus')->save($as);
1722                    array_push($updated, $achievementId);
1723                }
1724                $achievementId = 13;
1725                if ($acA['AchievementCondition']['value'] >= 85 && !isset($existingAs[$achievementId])) {
1726                    $as['AchievementStatus']['achievement_id'] = $achievementId;
1727                    ClassRegistry::init('AchievementStatus')->create();
1728                    ClassRegistry::init('AchievementStatus')->save($as);
1729                    array_push($updated, $achievementId);
1730                }
1731                $achievementId = 14;
1732                if ($acA['AchievementCondition']['value'] >= 95 && !isset($existingAs[$achievementId])) {
1733                    $as['AchievementStatus']['achievement_id'] = $achievementId;
1734                    ClassRegistry::init('AchievementStatus')->create();
1735                    ClassRegistry::init('AchievementStatus')->save($as);
1736                    array_push($updated, $achievementId);
1737                }
1738                $achievementId = 24;
1739                if ($acS['AchievementCondition']['value'] < 15 && !isset($existingAs[$achievementId])) {
1740                    $as['AchievementStatus']['achievement_id'] = $achievementId;
1741                    ClassRegistry::init('AchievementStatus')->create();
1742                    ClassRegistry::init('AchievementStatus')->save($as);
1743                    array_push($updated, $achievementId);
1744                }
1745                $achievementId = 25;
1746                if ($acS['AchievementCondition']['value'] < 10 && !isset($existingAs[$achievementId])) {
1747                    $as['AchievementStatus']['achievement_id'] = $achievementId;
1748                    ClassRegistry::init('AchievementStatus')->create();
1749                    ClassRegistry::init('AchievementStatus')->save($as);
1750                    array_push($updated, $achievementId);
1751                }
1752                $achievementId = 26;
1753                if ($acS['AchievementCondition']['value'] < 5 && !isset($existingAs[$achievementId])) {
1754                    $as['AchievementStatus']['achievement_id'] = $achievementId;
1755                    ClassRegistry::init('AchievementStatus')->create();
1756                    ClassRegistry::init('AchievementStatus')->save($as);
1757                    array_push($updated, $achievementId);
1758                }
1759            } elseif ($s['Set']['difficulty'] >= 1300 && $s['Set']['difficulty'] < 1500) {
1760                $achievementId = 15;
1761                if ($acA['AchievementCondition']['value'] >= 75 && !isset($existingAs[$achievementId])) {
1762                    $as['AchievementStatus']['achievement_id'] = $achievementId;
1763                    ClassRegistry::init('AchievementStatus')->create();
1764                    ClassRegistry::init('AchievementStatus')->save($as);
1765                    array_push($updated, $achievementId);
1766                }
1767                $achievementId = 16;
1768                if ($acA['AchievementCondition']['value'] >= 85 && !isset($existingAs[$achievementId])) {
1769                    $as['AchievementStatus']['achievement_id'] = $achievementId;
1770                    ClassRegistry::init('AchievementStatus')->create();
1771                    ClassRegistry::init('AchievementStatus')->save($as);
1772                    array_push($updated, $achievementId);
1773                }
1774                $achievementId = 17;
1775                if ($acA['AchievementCondition']['value'] >= 95 && !isset($existingAs[$achievementId])) {
1776                    $as['AchievementStatus']['achievement_id'] = $achievementId;
1777                    ClassRegistry::init('AchievementStatus')->create();
1778                    ClassRegistry::init('AchievementStatus')->save($as);
1779                    array_push($updated, $achievementId);
1780                }
1781                $achievementId = 27;
1782                if ($acS['AchievementCondition']['value'] < 18 && !isset($existingAs[$achievementId])) {
1783                    $as['AchievementStatus']['achievement_id'] = $achievementId;
1784                    ClassRegistry::init('AchievementStatus')->create();
1785                    ClassRegistry::init('AchievementStatus')->save($as);
1786                    array_push($updated, $achievementId);
1787                }
1788                $achievementId = 28;
1789                if ($acS['AchievementCondition']['value'] < 13 && !isset($existingAs[$achievementId])) {
1790                    $as['AchievementStatus']['achievement_id'] = $achievementId;
1791                    ClassRegistry::init('AchievementStatus')->create();
1792                    ClassRegistry::init('AchievementStatus')->save($as);
1793                    array_push($updated, $achievementId);
1794                }
1795                $achievementId = 29;
1796                if ($acS['AchievementCondition']['value'] < 8 && !isset($existingAs[$achievementId])) {
1797                    $as['AchievementStatus']['achievement_id'] = $achievementId;
1798                    ClassRegistry::init('AchievementStatus')->create();
1799                    ClassRegistry::init('AchievementStatus')->save($as);
1800                    array_push($updated, $achievementId);
1801                }
1802            } elseif ($s['Set']['difficulty'] >= 1500 && $s['Set']['difficulty'] < 1700) {
1803                $achievementId = 18;
1804                if ($acA['AchievementCondition']['value'] >= 75 && !isset($existingAs[$achievementId])) {
1805                    $as['AchievementStatus']['achievement_id'] = $achievementId;
1806                    ClassRegistry::init('AchievementStatus')->create();
1807                    ClassRegistry::init('AchievementStatus')->save($as);
1808                    array_push($updated, $achievementId);
1809                }
1810                $achievementId = 19;
1811                if ($acA['AchievementCondition']['value'] >= 85 && !isset($existingAs[$achievementId])) {
1812                    $as['AchievementStatus']['achievement_id'] = $achievementId;
1813                    ClassRegistry::init('AchievementStatus')->create();
1814                    ClassRegistry::init('AchievementStatus')->save($as);
1815                    array_push($updated, $achievementId);
1816                }
1817                $achievementId = 20;
1818                if ($acA['AchievementCondition']['value'] >= 95 && !isset($existingAs[$achievementId])) {
1819                    $as['AchievementStatus']['achievement_id'] = $achievementId;
1820                    ClassRegistry::init('AchievementStatus')->create();
1821                    ClassRegistry::init('AchievementStatus')->save($as);
1822                    array_push($updated, $achievementId);
1823                }
1824                $achievementId = 30;
1825                if ($acS['AchievementCondition']['value'] < 30 && !isset($existingAs[$achievementId])) {
1826                    $as['AchievementStatus']['achievement_id'] = $achievementId;
1827                    ClassRegistry::init('AchievementStatus')->create();
1828                    ClassRegistry::init('AchievementStatus')->save($as);
1829                    array_push($updated, $achievementId);
1830                }
1831                $achievementId = 31;
1832                if ($acS['AchievementCondition']['value'] < 20 && !isset($existingAs[$achievementId])) {
1833                    $as['AchievementStatus']['achievement_id'] = $achievementId;
1834                    ClassRegistry::init('AchievementStatus')->create();
1835                    ClassRegistry::init('AchievementStatus')->save($as);
1836                    array_push($updated, $achievementId);
1837                }
1838                $achievementId = 32;
1839                if ($acS['AchievementCondition']['value'] < 10 && !isset($existingAs[$achievementId])) {
1840                    $as['AchievementStatus']['achievement_id'] = $achievementId;
1841                    ClassRegistry::init('AchievementStatus')->create();
1842                    ClassRegistry::init('AchievementStatus')->save($as);
1843                    array_push($updated, $achievementId);
1844                }
1845            } else {
1846                $achievementId = 21;
1847                if ($acA['AchievementCondition']['value'] >= 75 && !isset($existingAs[$achievementId])) {
1848                    $as['AchievementStatus']['achievement_id'] = $achievementId;
1849                    ClassRegistry::init('AchievementStatus')->create();
1850                    ClassRegistry::init('AchievementStatus')->save($as);
1851                    array_push($updated, $achievementId);
1852                }
1853                $achievementId = 22;
1854                if ($acA['AchievementCondition']['value'] >= 85 && !isset($existingAs[$achievementId])) {
1855                    $as['AchievementStatus']['achievement_id'] = $achievementId;
1856                    ClassRegistry::init('AchievementStatus')->create();
1857                    ClassRegistry::init('AchievementStatus')->save($as);
1858                    array_push($updated, $achievementId);
1859                }
1860                $achievementId = 23;
1861                if ($acA['AchievementCondition']['value'] >= 95 && !isset($existingAs[$achievementId])) {
1862                    $as['AchievementStatus']['achievement_id'] = $achievementId;
1863                    ClassRegistry::init('AchievementStatus')->create();
1864                    ClassRegistry::init('AchievementStatus')->save($as);
1865                    array_push($updated, $achievementId);
1866                }
1867                $achievementId = 33;
1868                if ($acS['AchievementCondition']['value'] < 30 && !isset($existingAs[$achievementId])) {
1869                    $as['AchievementStatus']['achievement_id'] = $achievementId;
1870                    ClassRegistry::init('AchievementStatus')->create();
1871                    ClassRegistry::init('AchievementStatus')->save($as);
1872                    array_push($updated, $achievementId);
1873                }
1874                $achievementId = 34;
1875                if ($acS['AchievementCondition']['value'] < 20 && !isset($existingAs[$achievementId])) {
1876                    $as['AchievementStatus']['achievement_id'] = $achievementId;
1877                    ClassRegistry::init('AchievementStatus')->create();
1878                    ClassRegistry::init('AchievementStatus')->save($as);
1879                    array_push($updated, $achievementId);
1880                }
1881                $achievementId = 35;
1882                if ($acS['AchievementCondition']['value'] < 10 && !isset($existingAs[$achievementId])) {
1883                    $as['AchievementStatus']['achievement_id'] = $achievementId;
1884                    ClassRegistry::init('AchievementStatus')->create();
1885                    ClassRegistry::init('AchievementStatus')->save($as);
1886                    array_push($updated, $achievementId);
1887                }
1888            }
1889            $achievementId = 46;
1890            if ($acA['AchievementCondition']['value'] >= 100) {
1891                $ac100 = ClassRegistry::init('AchievementCondition')->find('all', ['conditions' => ['user_id' => Auth::getUserID(), 'category' => '%', 'value >=' => 100]]);
1892                if (!$ac100) {
1893                    $ac100 = [];
1894                }
1895                $ac100counter = 0;
1896                $ac100Count = count($ac100);
1897                for ($j = 0; $j < $ac100Count; $j++) {
1898                    if (count(TsumegoUtil::collectTsumegosFromSet($ac100[$j]['AchievementCondition']['set_id'])) >= 100) {
1899                        $ac100counter++;
1900                    }
1901                }
1902                $as100 = ClassRegistry::init('AchievementStatus')->find('first', ['conditions' => ['user_id' => Auth::getUserID(), 'achievement_id' => $achievementId]]);
1903                if ($as100 == null) {
1904                    $as['AchievementStatus']['achievement_id'] = $achievementId;
1905                    $as['AchievementStatus']['value'] = 1;
1906                    ClassRegistry::init('AchievementStatus')->create();
1907                    ClassRegistry::init('AchievementStatus')->save($as);
1908                    array_push($updated, $achievementId);
1909                } elseif ($as100['AchievementStatus']['value'] != $ac100counter) {
1910                    $as100['AchievementStatus']['value'] = $ac100counter;
1911                    ClassRegistry::init('AchievementStatus')->save($as100);
1912                    array_push($updated, $achievementId);
1913                }
1914            }
1915        }
1916        $updatedCount = count($updated);
1917        for ($i = 0; $i < $updatedCount; $i++) {
1918            $a = ClassRegistry::init('Achievement')->findById($updated[$i]);
1919            $updated[$i] = [];
1920            $updated[$i][0] = $a['Achievement']['name'];
1921            $updated[$i][1] = $a['Achievement']['description'];
1922            $updated[$i][2] = $a['Achievement']['image'];
1923            $updated[$i][3] = $a['Achievement']['color'];
1924            $updated[$i][4] = $a['Achievement']['xp'];
1925            $updated[$i][5] = $a['Achievement']['id'];
1926        }
1927
1928        return $updated;
1929    }
1930
1931    public static function updateXP($userID, $achievementData): void {
1932        $xpBonus = 0;
1933        $aCount = count($achievementData);
1934        for ($i = 0; $i < $aCount; $i++) {
1935            $xpBonus += $achievementData[$i][4];
1936        }
1937        if ($xpBonus == 0) {
1938            return;
1939        }
1940        $user = ClassRegistry::init('User')->findById($userID);
1941        $user['User']['xp'] = $xpBonus;
1942        Level::checkLevelUp($user['User']);
1943        ClassRegistry::init('User')->save($user);
1944    }
1945
1946    /**
1947     * @param int $uid User ID
1948     * @return void
1949     */
1950    protected function handleSearchSettings($uid) {
1951        $this->loadModel('UserContribution');
1952        $uc = $this->UserContribution->find('first', ['conditions' => ['user_id' => $uid]]);
1953        if ($uc == null) {
1954            $uc = [];
1955            $uc['UserContribution']['user_id'] = $uid;
1956            $uc['UserContribution']['added_tag'] = 0;
1957            $uc['UserContribution']['created_tag'] = 0;
1958            $uc['UserContribution']['made_proposal'] = 0;
1959            $uc['UserContribution']['reviewed'] = 0;
1960            $uc['UserContribution']['score'] = 0;
1961            $this->UserContribution->create();
1962            $this->UserContribution->save($uc);
1963        }
1964        new TsumegoFilters();
1965    }
1966
1967    protected function signIn(array $user): void {
1968        Auth::init($user);
1969        $vs = $this->TsumegoStatus->find('first', ['conditions' => ['user_id' => $user['User']['id']], 'order' => 'updated DESC']);
1970        if ($vs) {
1971            $this->Session->write('lastVisit', $vs['TsumegoStatus']['tsumego_id']);
1972        }
1973        $this->Session->write('texture', $user['User']['texture']);
1974        $this->Session->write('check1', $user['User']['id']);
1975    }
1976
1977    public function beforeFilter(): void {
1978        $this->loadModel('User');
1979        $this->loadModel('Activate');
1980        $this->loadModel('Tsumego');
1981        $this->loadModel('TsumegoRatingAttempt');
1982        $this->loadModel('Set');
1983        $this->loadModel('TimeModeAttempt');
1984        $this->loadModel('TsumegoStatus');
1985        $this->loadModel('Comment');
1986        $this->loadModel('TsumegoAttempt');
1987        $this->loadModel('AdminActivity');
1988        $this->loadModel('Achievement');
1989        $this->loadModel('AchievementStatus');
1990        $this->loadModel('AchievementCondition');
1991        $this->loadModel('SetConnection');
1992        $this->loadModel('Tag');
1993        $this->loadModel('TagName');
1994        $this->loadModel('Favorite');
1995
1996        Auth::init();
1997        $timeMode = new TimeMode();
1998
1999        $highscoreLink = 'highscore';
2000        $lightDark = 'light';
2001        $resetCookies = false;
2002        $levelBar = 1;
2003        $lastProfileLeft = 1;
2004        $lastProfileRight = 2;
2005        $hasFavs = false;
2006
2007        if (Auth::isLoggedIn()) {
2008            if (isset($_COOKIE['addTag']) && $_COOKIE['addTag'] != 0 && $this->Session->read('page') != 'set') {
2009                $newAddTag = explode('-', $_COOKIE['addTag']);
2010                $tagId = $newAddTag[0];
2011                $newTagName = $this->TagName->find('first', ['conditions' => ['name' => str_replace($tagId . '-', '', $_COOKIE['addTag'])]]);
2012                if ($newTagName) {
2013                    $saveTag = [];
2014                    $saveTag['Tag']['tag_name_id'] = $newTagName['TagName']['id'];
2015                    $saveTag['Tag']['tsumego_id'] = $tagId;
2016                    $saveTag['Tag']['user_id'] = Auth::getUserID();
2017                    $saveTag['Tag']['approved'] = 0;
2018                    $this->Tag->save($saveTag);
2019                }
2020                $this->set('removeCookie', 'addTag');
2021            }
2022            if (isset($_COOKIE['z_sess']) && $_COOKIE['z_sess'] != 0
2023            && strlen($_COOKIE['z_sess']) > 5) {
2024                Auth::getUser()['_sessid'] = $_COOKIE['z_sess'];
2025                Auth::saveUser();
2026            }
2027            if (Auth::getUser()['lastHighscore'] == 1) {
2028                $highscoreLink = 'highscore';
2029            } elseif (Auth::getUser()['lastHighscore'] == 2) {
2030                $highscoreLink = 'rating';
2031            } elseif (Auth::getUser()['lastHighscore'] == 3) {
2032                $highscoreLink = 'leaderboard';
2033            } elseif (Auth::getUser()['lastHighscore'] == 4) {
2034                $highscoreLink = 'highscore3';
2035            }
2036
2037            if (isset($_COOKIE['lastMode']) && $_COOKIE['lastMode'] != 0) {
2038                Auth::getUser()['lastMode'] = $_COOKIE['lastMode'];
2039                Auth::saveUser();
2040            }
2041            if (isset($_COOKIE['sound']) && $_COOKIE['sound'] != '0') {
2042                Auth::getUser()['sound'] = $_COOKIE['sound'];
2043                Auth::saveUser();
2044                unset($_COOKIE['sound']);
2045            }
2046            $this->set('ac', true);
2047            $this->set('user', Auth::getUser());
2048        }
2049
2050        if (isset($_COOKIE['lightDark']) && $_COOKIE['lightDark'] != '0') {
2051            $lightDark = $_COOKIE['lightDark'];
2052            if (Auth::isLoggedIn()) {
2053                // Convert string to integer for database storage
2054                $lightDarkInt = ($lightDark === 'light') ? 0 : 2;
2055                Auth::getUser()['lastLight'] = $lightDarkInt;
2056            }
2057        } elseif (Auth::isLoggedIn()) {
2058            if (Auth::getUser()['lastLight'] == 0
2059            || Auth::getUser()['lastLight'] == 1) {
2060                $lightDark = 'light';
2061            } else {
2062                $lightDark = 'dark';
2063            }
2064        }
2065
2066        if (Auth::isLoggedIn()) {
2067            $this->handleSearchSettings(Auth::getUserID());
2068            if (isset($_COOKIE['levelBar']) && $_COOKIE['levelBar'] != '0') {
2069                $levelBar = $_COOKIE['levelBar'];
2070                Auth::getUser()['levelBar'] = $levelBar;
2071            } elseif (Auth::getUser()['levelBar'] == 0
2072          || Auth::getUser()['levelBar'] == 'level') {
2073                $levelBar = 1;
2074            } else {
2075                $levelBar = 2;
2076            }
2077
2078            if (isset($_COOKIE['lastProfileLeft']) && $_COOKIE['lastProfileLeft'] != '0') {
2079                $lastProfileLeft = $_COOKIE['lastProfileLeft'];
2080                Auth::getUser()['lastProfileLeft'] = $lastProfileLeft;
2081            } else {
2082                $lastProfileLeft = Auth::getUser()['lastProfileLeft'];
2083                if ($lastProfileLeft == 0) {
2084                    $lastProfileLeft = 1;
2085                }
2086            }
2087            if (isset($_COOKIE['lastProfileRight']) && $_COOKIE['lastProfileRight'] != '0') {
2088                $lastProfileRight = $_COOKIE['lastProfileRight'];
2089                Auth::getUser()['lastProfileRight'] = $lastProfileRight;
2090            } else {
2091                $lastProfileRight = Auth::getUser()['lastProfileRight'];
2092                if ($lastProfileRight == 0) {
2093                    $lastProfileRight = 1;
2094                }
2095            }
2096        }
2097        $mode = 1;
2098        if (isset($_COOKIE['mode']) && $_COOKIE['mode'] != '0') {
2099            if ($_COOKIE['mode'] == 1) {
2100                $mode = 1;
2101            } else {
2102                $mode = 2;
2103            }
2104        }
2105
2106        if (Auth::isLoggedIn() && Auth::getUser()['mode'] == 2) {
2107            $mode = 2;
2108        }
2109
2110        if ($_COOKIE['sprint'] != 1) {
2111            $this->updateSprintCondition();
2112        }
2113
2114        if (Auth::isLoggedIn()) {
2115            if (isset($_COOKIE['revelation']) && $_COOKIE['revelation'] != 0) {
2116                Auth::getUser()['revelation'] -= 1;
2117            }
2118
2119            if (!$this->request->is('ajax')) {
2120                $this->PlayResultProcessor->checkPreviousPlay($timeMode);
2121            }
2122        }
2123        $boardNames = [];
2124        $enabledBoards = [];
2125        $boardPositions = [];
2126
2127        $boardNames[1] = 'Pine';
2128        $boardNames[2] = 'Ash';
2129        $boardNames[3] = 'Maple';
2130        $boardNames[4] = 'Shin Kaya';
2131        $boardNames[5] = 'Birch';
2132        $boardNames[6] = 'Wenge';
2133        $boardNames[7] = 'Walnut';
2134        $boardNames[8] = 'Mahogany';
2135        $boardNames[9] = 'Blackwood';
2136        $boardNames[10] = 'Marble 1';
2137        $boardNames[11] = 'Marble 2';
2138        $boardNames[12] = 'Marble 3';
2139        $boardNames[13] = 'Tibet Spruce';
2140        $boardNames[14] = 'Marble 4';
2141        $boardNames[15] = 'Marble 5';
2142        $boardNames[16] = 'Quarry 1';
2143        $boardNames[17] = 'Flowers';
2144        $boardNames[18] = 'Nova';
2145        $boardNames[19] = 'Spring';
2146        $boardNames[20] = 'Moon';
2147        $boardNames[21] = 'Apex';
2148        $boardNames[22] = 'Gold 1';
2149        $boardNames[23] = 'Amber';
2150        $boardNames[24] = 'Marble 6';
2151        $boardNames[25] = 'Marble 7';
2152        $boardNames[26] = 'Marble 8';
2153        $boardNames[27] = 'Marble 9';
2154        $boardNames[28] = 'Marble 10';
2155        $boardNames[29] = 'Jade';
2156        $boardNames[30] = 'Quarry 2';
2157        $boardNames[31] = 'Black Bricks';
2158        $boardNames[32] = 'Wallpaper 1';
2159        $boardNames[33] = 'Wallpaper 2';
2160        $boardNames[34] = 'Gold & Gray';
2161        $boardNames[35] = 'Gold & Pink';
2162        $boardNames[36] = 'Veil';
2163        $boardNames[37] = 'Tiles';
2164        $boardNames[38] = 'Mars';
2165        $boardNames[39] = 'Pink Cloud';
2166        $boardNames[40] = 'Reptile';
2167        $boardNames[41] = 'Mezmerizing';
2168        $boardNames[42] = 'Magenta Sky';
2169        $boardNames[43] = 'Tsumego Hero';
2170        $boardNames[44] = 'Pretty';
2171        $boardNames[45] = 'Hunting';
2172        $boardNames[46] = 'Haunted';
2173        $boardNames[47] = 'Carnage';
2174        $boardNames[48] = 'Blind Spot';
2175        $boardNames[49] = 'Giants';
2176        $boardNames[50] = 'Gems';
2177        $boardNames[51] = 'Grandmaster';
2178        $boardPositions[1] = [1, 'texture1', 'black34.png', 'white34.png'];
2179        $boardPositions[2] = [2, 'texture2', 'black34.png', 'white34.png'];
2180        $boardPositions[3] = [3, 'texture3', 'black34.png', 'white34.png'];
2181        $boardPositions[4] = [4, 'texture4', 'black.png', 'white.png'];
2182        $boardPositions[5] = [5, 'texture5', 'black34.png', 'white34.png'];
2183        $boardPositions[6] = [6, 'texture6', 'black.png', 'white.png'];
2184        $boardPositions[7] = [7, 'texture7', 'black34.png', 'white34.png'];
2185        $boardPositions[8] = [8, 'texture8', 'black.png', 'white.png'];
2186        $boardPositions[9] = [9, 'texture9', 'black.png', 'white.png'];
2187        $boardPositions[10] = [10, 'texture10', 'black34.png', 'white34.png'];
2188        $boardPositions[11] = [11, 'texture11', 'black34.png', 'white34.png'];
2189        $boardPositions[12] = [12, 'texture12', 'black34.png', 'white34.png'];
2190        $boardPositions[13] = [13, 'texture13', 'black34.png', 'white34.png'];
2191        $boardPositions[14] = [14, 'texture14', 'black34.png', 'white34.png'];
2192        $boardPositions[15] = [15, 'texture15', 'black.png', 'white.png'];
2193        $boardPositions[16] = [16, 'texture16', 'black34.png', 'white34.png'];
2194        $boardPositions[17] = [17, 'texture17', 'black34.png', 'white34.png'];
2195        $boardPositions[18] = [18, 'texture18', 'black.png', 'white.png'];
2196        $boardPositions[19] = [19, 'texture19', 'black34.png', 'white34.png'];
2197        $boardPositions[20] = [20, 'texture20', 'black34.png', 'white34.png'];
2198        $boardPositions[21] = [33, 'texture33', 'black34.png', 'white34.png'];
2199        $boardPositions[22] = [21, 'texture21', 'black.png', 'whiteKo.png'];
2200        $boardPositions[23] = [22, 'texture22', 'black34.png', 'white34.png'];
2201        $boardPositions[24] = [34, 'texture34', 'black.png', 'white.png'];
2202        $boardPositions[25] = [35, 'texture35', 'black34.png', 'white34.png'];
2203        $boardPositions[26] = [36, 'texture36', 'black.png', 'white.png'];
2204        $boardPositions[27] = [37, 'texture37', 'black34.png', 'white34.png'];
2205        $boardPositions[28] = [38, 'texture38', 'black38.png', 'white34.png'];
2206        $boardPositions[29] = [39, 'texture39', 'black.png', 'white.png'];
2207        $boardPositions[30] = [40, 'texture40', 'black34.png', 'white34.png'];
2208        $boardPositions[31] = [41, 'texture41', 'black34.png', 'white34.png'];
2209        $boardPositions[32] = [42, 'texture42', 'black34.png', 'white42.png'];
2210        $boardPositions[33] = [43, 'texture43', 'black34.png', 'white42.png'];
2211        $boardPositions[34] = [44, 'texture44', 'black34.png', 'white34.png'];
2212        $boardPositions[35] = [45, 'texture45', 'black34.png', 'white42.png'];
2213        $boardPositions[36] = [47, 'texture47', 'black34.png', 'white34.png'];
2214        $boardPositions[37] = [48, 'texture48', 'black34.png', 'white34.png'];
2215        $boardPositions[38] = [49, 'texture49', 'black.png', 'white.png'];
2216        $boardPositions[39] = [50, 'texture50', 'black34.png', 'white34.png'];
2217        $boardPositions[40] = [51, 'texture51', 'black34.png', 'white34.png'];
2218        $boardPositions[41] = [52, 'texture52', 'black34.png', 'white34.png'];
2219        $boardPositions[42] = [53, 'texture53', 'black34.png', 'white34.png'];
2220        $boardPositions[43] = [54, 'texture54', 'black54.png', 'white54.png'];
2221        $boardPositions[44] = [23, 'texture23', 'black.png', 'whiteFlower.png'];
2222        $boardPositions[45] = [24, 'texture24', 'black24.png', 'white24.png'];
2223        $boardPositions[46] = [25, 'texture25', 'blackGhost.png', 'white.png'];
2224        $boardPositions[47] = [26, 'texture26', 'blackInvis.png', 'whiteCarnage.png'];
2225        $boardPositions[48] = [27, 'texture27', 'black27.png', 'white27.png'];
2226        $boardPositions[49] = [28, 'texture28', 'blackGiant.png', 'whiteKo.png'];
2227        $boardPositions[50] = [29, 'texture29', 'blackKo.png', 'whiteKo.png'];
2228        $boardPositions[51] = [30, 'texture55', 'blackGalaxy.png', 'whiteGalaxy.png'];
2229
2230        $boardCount = 51;
2231
2232        if ($this->Session->check('texture') || (isset($_COOKIE['texture']) && $_COOKIE['texture'] != '0')) {
2233            $splitCookie = [];
2234            if (isset($_COOKIE['texture']) && $_COOKIE['texture'] != '0') {
2235                $splitCookie = str_split($_COOKIE['texture']);
2236                $textureCookies = $_COOKIE['texture'];
2237                Auth::getUser()['texture'] = $this->Session->read('texture');
2238                $this->Session->write('texture', $_COOKIE['texture']);
2239                $this->set('textureCookies', $textureCookies);
2240            } else {
2241                if (Auth::isLoggedIn()) {
2242                    $this->Session->write('texture', Auth::getUser()['texture']);
2243                }
2244                $textureCookies = $this->Session->read('texture');
2245                $splitTextureCookies = str_split($textureCookies);
2246                $splitTextureCookiesCount = count($splitTextureCookies);
2247                for ($i = 0; $i < $splitTextureCookiesCount; $i++) {
2248                    if ($splitTextureCookies[$i] == 2) {
2249                        $enabledBoards[$i + 1] = 'checked';
2250                    } else {
2251                        $enabledBoards[$i + 1] = '';
2252                    }
2253                }
2254            }
2255
2256            $splitCookieCount = count($splitCookie);
2257            for ($i = 0; $i < $splitCookieCount; $i++) {
2258                if ($splitCookie[$i] == 2) {
2259                    $enabledBoards[$i + 1] = 'checked';
2260                } else {
2261                    $enabledBoards[$i + 1] = '';
2262                }
2263            }
2264            if (Auth::isLoggedIn()) {
2265                Auth::saveUser();
2266            }
2267        }
2268
2269        if (!$this->Session->check('texture')) {
2270            $this->Session->write('texture', '222222221111111111111111111111111111111111111111111');
2271            $enabledBoards[1] = 'checked';
2272            $enabledBoards[2] = 'checked';
2273            $enabledBoards[3] = 'checked';
2274            $enabledBoards[4] = 'checked';
2275            $enabledBoards[5] = 'checked';
2276            $enabledBoards[6] = 'checked';
2277            $enabledBoards[7] = 'checked';
2278            $enabledBoards[8] = 'checked';
2279            $enabledBoards[9] = '';
2280            $enabledBoards[10] = '';
2281            $enabledBoards[11] = '';
2282            $enabledBoards[12] = '';
2283            $enabledBoards[13] = '';
2284            $enabledBoards[14] = '';
2285            $enabledBoards[15] = '';
2286            $enabledBoards[16] = '';
2287            $enabledBoards[17] = '';
2288            $enabledBoards[18] = '';
2289            $enabledBoards[19] = '';
2290            $enabledBoards[20] = '';
2291            $enabledBoards[21] = '';
2292            $enabledBoards[22] = '';
2293            $enabledBoards[23] = '';
2294            $enabledBoards[24] = '';
2295            $enabledBoards[25] = '';
2296            $enabledBoards[26] = '';
2297            $enabledBoards[27] = '';
2298            $enabledBoards[28] = '';
2299            $enabledBoards[29] = '';
2300            $enabledBoards[30] = '';
2301            $enabledBoards[31] = '';
2302            $enabledBoards[32] = '';
2303            $enabledBoards[33] = '';
2304            $enabledBoards[34] = '';
2305            $enabledBoards[35] = '';
2306            $enabledBoards[36] = '';
2307            $enabledBoards[37] = '';
2308            $enabledBoards[38] = '';
2309            $enabledBoards[39] = '';
2310            $enabledBoards[40] = '';
2311            $enabledBoards[41] = '';
2312            $enabledBoards[42] = '';
2313            $enabledBoards[43] = '';
2314            $enabledBoards[44] = '';
2315            $enabledBoards[45] = '';
2316            $enabledBoards[46] = '';
2317            $enabledBoards[47] = '';
2318            $enabledBoards[48] = '';
2319            $enabledBoards[49] = '';
2320            $enabledBoards[50] = '';
2321            $enabledBoards[51] = '';
2322        }
2323        $achievementUpdate = [];
2324        if ($this->Session->check('initialLoading')) {
2325            $achievementUpdate1 = $this->checkLevelAchievements();
2326            $achievementUpdate2 = $this->checkProblemNumberAchievements();
2327            $achievementUpdate3 = $this->checkRatingAchievements();
2328            $achievementUpdate4 = $this->checkTimeModeAchievements();
2329            $achievementUpdate5 = $this->checkDanSolveAchievements();
2330            $achievementUpdate = array_merge(
2331                $achievementUpdate1 ?: [],
2332                $achievementUpdate2 ?: [],
2333                $achievementUpdate3 ?: [],
2334                $achievementUpdate4 ?: [],
2335                $achievementUpdate5 ?: []
2336            );
2337            $this->Session->delete('initialLoading');
2338        }
2339
2340        if (count($achievementUpdate) > 0) {
2341            $this->updateXP(Auth::getUserID(), $achievementUpdate);
2342        }
2343
2344        $nextDay = new DateTime('tomorrow');
2345        if (Auth::isLoggedIn()) {
2346            Auth::getUser()['name'] = $this->checkPicture(Auth::getUser());
2347            $this->set('user', Auth::getUser());
2348        }
2349        $this->set('mode', $mode);
2350        $this->set('nextDay', $nextDay->format('m/d/Y'));
2351        $this->set('boardNames', $boardNames);
2352        $this->set('enabledBoards', $enabledBoards);
2353        $this->set('boardPositions', $boardPositions);
2354        $this->set('highscoreLink', $highscoreLink);
2355        $this->set('achievementUpdate', $achievementUpdate);
2356        $this->set('lightDark', $lightDark);
2357        $this->set('levelBar', $levelBar);
2358        $this->set('lastProfileLeft', $lastProfileLeft);
2359        $this->set('lastProfileRight', $lastProfileRight);
2360        $this->set('resetCookies', $resetCookies);
2361        $this->set('hasFavs', $hasFavs);
2362    }
2363
2364    public function afterFilter() {}
2365}